Created
June 14, 2010 01:32
-
-
Save danott/437166 to your computer and use it in GitHub Desktop.
Post to Tumblr from Expression Engine using the Tumblr API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Daniel Ott | |
* 13 June 2010 | |
* A plugin that you can use to send stuff to Tumblr from Expression Engine | |
* using Tumblr's API | |
* | |
* Very minimal for right now. You could expand it to send categories using | |
* the API's "tags" option, send different types of posts, etc. | |
* | |
* Make sure to supply your email and password to make it work. | |
*/ | |
$plugin_info = array( | |
'pi_name' => 'Post to Tumblr', | |
'pi_version' => '0.1', | |
'pi_author' => 'Daniel Ott', | |
'pi_author_url' => 'http://danott.us/', | |
'pi_description' => 'A big expiriment', | |
'pi_usage' => Post_to_tumblr::usage() | |
); | |
class Post_to_tumblr { | |
var $return_data; | |
/** ---------------------------------------- | |
/** Post to Tumblr! | |
/** ----------------------------------------*/ | |
function Post_to_tumblr($str = '') | |
{ | |
global $TMPL, $FNS; | |
$title = ( ! $TMPL->fetch_param('title')) ? 'untitled' : $TMPL->fetch_param('title'); | |
$date = ( ! $TMPL->fetch_param('date')) ? now() : $TMPL->fetch_param('date'); | |
if ($str == '') | |
{ | |
$str = $TMPL->tagdata; | |
} | |
// Authorization info | |
$tumblr_email = 'your@email.com'; | |
$tumblr_password = 'supersecretpasswordshhhhhh'; | |
// Data for new record | |
$post_type = 'regular'; | |
$post_title = $title; | |
$post_body = $str; | |
// Prepare POST request | |
$request_data = http_build_query( | |
array( | |
'email' => $tumblr_email, | |
'password' => $tumblr_password, | |
'type' => $post_type, | |
'title' => $post_title, | |
'body' => $post_body, | |
'date' => $date, | |
//'state' => 'draft', | |
'generator' => 'Expression Engine' | |
) | |
); | |
// Send the POST request (with cURL) | |
$c = curl_init('http://www.tumblr.com/api/write'); | |
curl_setopt($c, CURLOPT_POST, true); | |
curl_setopt($c, CURLOPT_POSTFIELDS, $request_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) | |
{ | |
$this->return_data = "Success! The new post ID is $result.\n"; | |
} | |
else if ($status == 403) | |
{ | |
$this->return_data = 'Bad email or password'; | |
} | |
else | |
{ | |
$this->return_data = "Error: $result\n"; | |
} | |
} | |
/* END */ | |
// ---------------------------------------- | |
// Plugin Usage | |
// ---------------------------------------- | |
// This function describes how the plugin is used. | |
// Make sure and use output buffering | |
function usage() | |
{ | |
ob_start(); | |
?> | |
Send stuff to tumblr. | |
Make sure you put your email and password in the plugin | |
Example Template | |
---------------- | |
{exp:weblog:entries weblog="default_weblog" limit="15"} | |
{exp:post_to_tumblr title="{title}" date="{entry_date format="{DATE_ISO8601}"}"}{body}{/exp:post_to_tumblr} | |
{/exp:weblog:entries} | |
<?php | |
$buffer = ob_get_contents(); | |
ob_end_clean(); | |
return $buffer; | |
} | |
/* END */ | |
} | |
// END CLASS | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment