¿Eres nuevo? ¡Lee el FAQ y ponte al día!
6k visitas

Script to export Wordpress posts to Tumblr

Tumblr does not provide a way to import posts from Wordpress. To solve that, I fixed some code I found on the internet and it seems to work. Here it is:

PHP
  1. <?php
  2.  
  3. // wp-tumblr.php
  4. // originally by miguel santirso, http://miguelsantirso.es
  5. // updated for wp 1.1 xml export format by christopher j. pilkington, http://0x1.net, 2011 oct 27
  6. //
  7. // Use at your own risk, you may want to test this with a throw-away tumblr account first.
  8. //
  9.  
  10. // The full path to the XML file you exported from Wordpress
  11. $xmlFile         = 'MYEXPORTFILE.xml';
  12.  
  13. // Your tumblr log in details
  14. $tumblr_email    = 'EMAIL';
  15. $tumblr_password = 'PASSWORD';
  16.  
  17. // Tumblr URL (e.g. http://yourname.tumblr.com)
  18. $tumblrUrl       = 'http://MYTUMBLRID.tumblr.com';
  19.  
  20. // If WordPress post is a draft, true to upload as private, false to not upload it.
  21. $publishDraftAsPrivate = true;
  22.  
  23. // A writeable logfile, to keep track of the new URLs.
  24. $logFile         = 'log.txt';
  25.  
  26. if (file_exists($xmlFile)) {
  27.     $xml = simplexml_load_file($xmlFile);
  28. } else {
  29.     echo "ERROR: no such file\n\n";
  30.     die();
  31. }
  32.  
  33. if (isset($xml)) {
  34.  
  35.     $nodes = $xml->xpath('/rss/channel/item');
  36.  
  37.     $count = 0;
  38.  
  39.     while(list( , $node) = each($nodes)) {
  40.  
  41.         $post_type =  'regular';
  42.         $post_title = $node->title;
  43.         $post_title = str_replace("%20"," ",$post_title);
  44.         $content =    $node->children("http://purl.org/rss/1.0/modules/content/");            
  45.         $post_body = (string)$content->encoded;    
  46.         $post_body = str_replace(""," ",$post_body);
  47.         $wp =        $node->children("http://wordpress.org/export/1.1/");
  48.         $date =      $node->pubDate;
  49.         echo $date . "\n";
  50.         $private = 0;
  51.  
  52.         if ($wp->status != "publish" && $wp->status != "inherit") {
  53.  
  54.             if (!$publishDraftAsPrivate) {
  55.                 continue;
  56.             }
  57.  
  58.             $private = 1;
  59.         }
  60.  
  61.         if ($wp->post_type == "attachment")
  62.             continue;
  63.  
  64.         $count++;
  65.  
  66.         $request = array(
  67.             'email'     => $tumblr_email,
  68.             'password'  => $tumblr_password,
  69.             'type'      => $post_type,
  70.             'title'     => $post_title,
  71.             'date'      => $date,
  72.             'body'      => $post_body,
  73.             'generator' => 'wptumblr-ds',
  74.             'private'   => $private
  75.         );
  76.  
  77.         $request_data = "";
  78.  
  79.         $first = true;
  80.         foreach ($request as $key=>$value) {
  81.  
  82.             if ($first) {
  83.                 $first = false;
  84.             } else {
  85.                 $request_data .= "&";          
  86.             }
  87.  
  88.             $request_data .= urlencode($key) . "=" . urlencode($value);
  89.         }
  90.  
  91.         $c = curl_init('http://www.tumblr.com/api/write');
  92.         curl_setopt($c, CURLOPT_POST, true);
  93.         curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
  94.         curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  95.         $result = curl_exec($c);
  96.         $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  97.         curl_close($c);
  98.  
  99.         if ($status == 201) {
  100.             echo "Success! Post ID: $result\n";
  101.  
  102.             $res = file_put_contents($logFile, $node->link . " : " . $tumblrUrl . "/post/" . $result, FILE_APPEND);          
  103.         } else if ($status == 403) {
  104.             echo 'ERROR: Bad email and/or password.';
  105.             die(); // Bad credentials are fatal.
  106.         } else {
  107.             echo "Error: $result\n"; // This might not be fatal.
  108.         }
  109.     }  
  110. }
  111.  
  112. ?>
  113.  

­

If you find some bug on this code or you have some suggestion, you can email me: miguel.santirso at gmail.com.

Etiquetas: PHP api wordpress tumblr import-export

Insertar: