Jamesking56
2/12/2013 - 10:25 PM

Wordpress to Tumblr

Wordpress to Tumblr

<?php
$req_url = 'http://www.tumblr.com/oauth/request_token';
$authurl = 'http://www.tumblr.com/oauth/authorize';
$acc_url = 'http://www.tumblr.com/oauth/access_token';

$conskey = 'YOUR CONSUMER KEY';
$conssec = 'YOUR CONSUMER SECRET';

session_start();

// In state=1 the next request should include an oauth_token.
// If it doesn't go back to 0
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;
try {
  $oauth = new OAuth($conskey,$conssec);
  $oauth->enableDebug();
  if(!isset($_GET['oauth_token']) && !$_SESSION['state']) {
    $request_token_info = $oauth->getRequestToken($req_url, 'http://www.example.com/oauth.php');
    $_SESSION['secret'] = $request_token_info['oauth_token_secret'];
    $_SESSION['state'] = 1;

    //Make authorize request with oauth_token
    header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
    exit;
  } else if($_SESSION['state']==1) {

    //Callback code

    $oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
    $access_token_info = $oauth->getAccessToken($acc_url);
    $_SESSION['state'] = 2;
    $_SESSION['token'] = $access_token_info['oauth_token'];
    $_SESSION['secret'] = $access_token_info['oauth_token_secret'];

  } 

  //Make tumblr post request
  $oauth->setToken($_SESSION['token'],$_SESSION['secret']);

  require_once("Wordpress.class.php");
  $wordpress = new WordPress("YOUR_WORDPRESS_XML_FILE_URL");
  $posts = $wordpress->getPosts();

  foreach($posts as $post)
  {
     $oauth->fetch("http://api.tumblr.com/v2/blog/YOUR_TUMBLR_URL/post", array('type'=>'text', 'state' => 'published', 'tags'=>$post['categories'], 'tweet'=>'off', 'date'=>$post['pubDate'], 'format'=>'html', 'title'=>$post['title'], 'body'=>$post['content'], 'slug'=>$post['slug']), OAUTH_HTTP_METHOD_POST);
     echo $oauth->getLastResponse();
  }
} catch(OAuthException $E) {
  print_r($E);
}
?>