Posted By

shakefon on 04/30/09


Tagged

wordpress api convert tumblr


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

1man
fruehjahr
BrianCoyDesign
kantiano
alolve


Migrate Wordpress to Tumblr


 / Published in: PHP
 

URL: http://shakefon.tumblr.com

Recently moved my Wordpress blog to a tumblr account while I was removing a hosting account, wrote this script to do the transfer for me using Wordpress' Export feature and Tumblr's API.

It's rough around the edges but gets the job done!

  1. <?php
  2.  
  3. // The full path to the XML file you exported from Wordpress
  4. $xmlFile = '';
  5. // Your tumblr log in details
  6. $tumblr_email = '';
  7. $tumblr_password = '';
  8. // Tumblr URL (e.g. http://yourname.tumblr.com)
  9. $tumblrUrl = '';
  10. // If a post from Wordpress is a draft, do you want it posted as private so you // have it available? True if so, False to ignore drafts
  11. $publishDraftAsPrivate = true;
  12. // Full path to a file that is writable, so that a log of current URL on your // wordpress blog to new URL on your tumblr can be written (good for redirects // to preserve links, etc)
  13. $logFile = '';
  14.  
  15. if (file_exists($xmlFile)) {
  16.  
  17. $xml = simplexml_load_file($xmlFile);
  18.  
  19. } else {
  20.  
  21. echo "no such file";
  22.  
  23. }
  24.  
  25. if (isset($xml)) {
  26.  
  27. $nodes = $xml->xpath('/rss/channel/item');
  28.  
  29. $count = 0;
  30.  
  31. while(list( , $node) = each($nodes)) {
  32.  
  33. $post_type = 'regular';
  34. $post_title = rawurlencode($node->title);
  35.  
  36. $post_title = str_replace("%20"," ",$post_title);
  37.  
  38. $content = $node->children("http://purl.org/rss/1.0/modules/content/");
  39.  
  40. $post_body = (string)$content->encoded;
  41.  
  42. $publish_status = $node->children("http://wordpress.org/export/1.0/");
  43.  
  44. $private = 0;
  45.  
  46. if ($publish_status->status != "publish") {
  47.  
  48. if (!$publishDraftAsPrivate) {
  49.  
  50. continue;
  51.  
  52. }
  53.  
  54. $private = 1;
  55.  
  56. }
  57.  
  58. $count++;
  59.  
  60. $request = array(
  61. 'email' => $tumblr_email,
  62. 'password' => $tumblr_password,
  63. 'type'=> $post_type,
  64. 'title'=>$post_title,
  65. 'body'=>$post_body,
  66. 'generator'=> 'wptumblr-ds',
  67. 'private'=>$private
  68. );
  69.  
  70. $request_data = "";
  71.  
  72. $first = true;
  73. foreach ($request as $key=>$value) {
  74.  
  75. if ($first) {
  76. $first = false;
  77. } else {
  78. $request_data .= "&";
  79. }
  80.  
  81. $request_data .= urlencode($key) . "=";
  82.  
  83. if ($key == "body") {
  84.  
  85. $request_data .= urldecode($value);
  86.  
  87. } else {
  88.  
  89. $request_data .= urlencode($value);
  90.  
  91. }
  92.  
  93. }
  94.  
  95. $c = curl_init('http://www.tumblr.com/api/write');
  96. curl_setopt($c, CURLOPT_POST, true);
  97. curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
  98. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  99. $result = curl_exec($c);
  100. $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  101. curl_close($c);
  102.  
  103. if ($status == 201) {
  104. echo "Success! Post ID: $result";
  105.  
  106. $res = file_put_contents($logFile,$node->link . " : " . $tumblrUrl . "/post/" . $result,FILE_APPEND);
  107. } else if ($status == 403) {
  108. echo 'Bad email/password';
  109. } else {
  110. echo "Error: $result\n";
  111. }
  112.  
  113. }
  114.  
  115. }
  116.  
  117. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: preshit on May 10, 2009

How exactly does the path to the XML file work? I have uploaded it to my server, made it readable to the world, but the script still says no such file.

Posted By: 8bitkid on October 16, 2009

i keep getting the error: Call to a member function xpath() on a non-object in /home/max/Desktop/wp2tumblr.php on line 27

i imagine this is related to $nodes = $xml->xpath('/rss/channel/item'); but am not experienced enough to solve the problem. any help is greatly appreciated.

Posted By: 8bitkid on October 16, 2009

i keep getting the error: Call to a member function xpath() on a non-object in /home/max/Desktop/wp2tumblr.php on line 27

i imagine this is related to $nodes = $xml->xpath('/rss/channel/item'); but am not experienced enough to solve the problem. any help is greatly appreciated.

Posted By: 8bitkid on October 16, 2009

sorry for the double-post. browser wonkiness :-)

Posted By: miguelSantirso on April 17, 2010

Hey! This script has some problems with special characters in the titles of the posts. I have fixed that and added two improvements: Sets the date of the posts in tumblr to the original date of the posts and it does not publish the Wordpress attachments as tumblr posts. I published the modified script at The Source Cookbook: Script to export Wordpress posts to tumblr

Posted By: ronCYA on January 15, 2011

In reply to preshit: Even though it states 'full path', it actually works better if you just include all your files in the same folder and DON'T specify the full path. At first I tried "http://blah.com/blahblah.bla" for the XML WordPress file and log file, but that came up with "no such file". Once I just changed it to "blahblah.bla" it worked just fine :)

You need to login to post a comment.