-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathwordpressToTumblr.php
125 lines (99 loc) · 3.18 KB
/
wordpressToTumblr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
// Set the following parameters
$tumblrEmail = '';
$tumblrPassword = '';
$file = '';
$tumblr = new Tumblr();
$tumblr->setCredentials($tumblrEmail, $tumblrPassword);
$wpt = new WordpressToTumblr();
$wpt->setWordpressXmlFile($file);
$wpt->loadXML();
$wpt->setTumblrObject($tumblr);
$wpt->postToTumblr();
class WordpressToTumblr {
private $wpExportFile;
private $xml;
private $tumblr;
public function setWordpressXmlFile($file) {
$this->wpExportFile = $file;
}
public function loadXML() {
$this->xml = simplexml_load_file($this->wpExportFile);
}
public function setTumblrObject($obj) {
$this->tumblr = $obj;
}
public function postToTumblr() {
$ns = $this->xml->getNamespaces(true);
foreach ($this->xml->channel[0]->item as $item) {
$title = (string)$item->title;
$body = (string)$item->children($ns['content'])->encoded;
$pubDate = (string)$item->children($ns['wp'])->post_date;
$this->tumblr->postRegular( $title , $body, array('date' => $pubDate));
}
}
}
class Tumblr {
private $pass;
private $email;
private $baseUrl = 'http://www.tumblr.com/api/';
private $postParameters = array(
'type' => null,
'generator' => null,
'date' => null,
'private' => '0',
'tags' => null,
'format' => 'html',
'group' => null,
'slug' => null,
'state' => 'published',
'send-to-twitter' => 'no'
);
public function setCredentials($user, $pass) {
$this->email = $user;
$this->pass = $pass;
}
private function write($postType, $params = array()) {
if (empty($params)) {
return false;
}
// add tumblr credentials
$params = array_merge(
$params, array('email' => $this->email, 'password' => $this->pass)
);
$tumblrURL = $this->baseUrl . 'write';
return $this->executeRequest($tumblrURL, $params);
}
public function postRegular($title, $body, $additionalParams = array()) {
if (empty($title) || empty($body)) {
return false;
}
$params = array_intersect_key($additionalParams, $this->postParameters);
$params = array_merge(
$params, array('title' => $title, 'body' => $body, 'type' => 'regular')
);
return $this->write('regular',$params);
}
private function executeRequest($url, $params) {
$data = http_build_query($params);
$c = curl_init($url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
echo "Success! The request executed succesfully.\n";
return $result;
} else if ($status == 403) {
echo 'Bad email or password';
return false;
} else {
echo "Error: $result\n";
return false;
}
}
}
?>