Tumblr API CodeIgniter Library

May 31st, 2011 - Posted by Steve Marks to PHP, Web Development.

Tumblr API CodeIgniter Library

Over the past few years Tumblr has put up a good battle against other blogging platforms and still stands strong against the likes of WordPress and Blogger. Not wanting to be narrow-minded I decided to take a look into Tumblr and was actually very impressed. I don’t think I would consider using it myself (yet) but it’s clean layout and ease of use will definitely keep me checking back once in a while.

Whilst playing with the Tumblr interface I noticed they provide a pretty nifty API that allows developers to read, create, edit and delete posts from a users blog. Already being a massive fan of the PHP framework CodeIgniter, and a potential Tumblr convert, I decided to take up the challenge of writing a CodeIgniter library that easily allows these Tumblr API calls to be made through the framework. And so I give you first release…

Downloading and Installing the Library

You can download the Tumblr API library from the following location:

Download Library

View GitHub Repository

Within the ZIP there is a config file containing various authentication and display settings, and the library itself. These should be placed into ‘application/config’ and ‘application/libraries’ respectively.

Using the Library

Using the library is relatively straight-forward with most actions only taking a few lines of code. I will provide more in-depth documentation very soon but for now let’s take a look at a few examples. The Tumblr API documentation may also help a little.

Reading Posts

The tumblr.php config file in the ‘application/config’ directory:

// URL of tumblr blog.
// Replace to be the URL of the blog you want to read posts from
$config['tumblr_blog_url'] = 'http://[YOU].tumblr.com';

The tumblr.php library file in the ‘application/libraries’ directory:

// Load the library
$this->load->library('tumblr');

// Obtain an array of posts from the specified blog
// See the config file for a list of settings available
$posts = $this->tumblr->read_posts();

// Output the posts
print_r($posts);

Example output:

Array
(
   [0] => Array
   (
      [title] => This is a sample post title
      [body] => <p>And this is the body</p>
      [id] => 6010787147
      [url] => http://[YOU].tumblr.com/post/6010787147
      [url-with-slug] => http://[YOU].tumblr.com/post/6010787147/post-1
      [type] => regular
      [date-gmt] => 2011-05-30 20:41:17 GMT
      [date] => Mon, 30 May 2011 16:41:17
      [unix-timestamp] => 1306788077
      [format] => html
      [reblog-key] => Wfd5YDHP
      [slug] => post-1
   )
   [1] => Array
   (
      [title] => And another post
      [body] => <p>More lovely post content</p>
      [id] => 6011797127
      [url] => http://[YOU].tumblr.com/post/6011797127
      [url-with-slug] => http://[YOU].tumblr.com/post/6011797127/post-2
      [type] => regular
      [date-gmt] => 2011-05-29 20:43:17 GMT
      [date] => Sun, 29 May 2011 16:43:17
      [unix-timestamp] => 1306788077
      [format] => html
      [reblog-key] => Wfd4XDVP
      [slug] => post-2
   )
)

Adding New Posts

The tumblr.php config file in the ‘application/config’ directory:

// Email address and Password used to log in to Tumblr.
$config['tumblr_email'] = '[YOUR-EMAIL]';
$config['tumblr_password'] = '[YOUR-PASSWORD]';

The tumblr.php library file in the ‘application/libraries’ directory:

// Load the library
$this->load->library('tumblr');

// Add the post
$post_data = array();
$post_data['type'] = 'regular';
$post_data['title'] = 'This is a post created through the API';
$post_data['body'] = 'And this is the body';
$post_id = $this->tumblr->write_post($post_data);

// Output the newly created post ID
echo $post_id;

Editing Existing Posts

The tumblr.php config file in the ‘application/config’ directory:

// Email address and Password used to log in to Tumblr.
$config['tumblr_email'] = '[YOUR-EMAIL]';
$config['tumblr_password'] = '[YOUR-PASSWORD]';

The tumblr.php library file in the ‘application/libraries’ directory:

// Load the library
$this->load->library('tumblr');

// Add the post
$post_data = array();
$post_data['post-id'] = '6010787147'; // The ID of the post that we want to edit
$post_data['type'] = 'regular';
$post_data['title'] = 'This is an editing post updated through the API';
$post_data['body'] = 'And even an updated body';
$this->tumblr->write_post($post_data);

Deleting Posts

The tumblr.php config file in the ‘application/config’ directory:

// Email address and Password used to log in to Tumblr.
$config['tumblr_email'] = '[YOUR-EMAIL]';
$config['tumblr_password'] = '[YOUR-PASSWORD]';

The tumblr.php library file in the ‘application/libraries’ directory:

// Load the library
$this->load->library('tumblr');

// Delete the post
$this->tumblr->delete_post('6010787147');

I will be adding more examples and documentation very soon so be sure to check back. Alternatively please leave a comment if you have any questions or feedback about the library.

This entry was posted on Tuesday, May 31st, 2011 at 10:55 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (1)
  1. kauffman says:

    this is a great library.. thanks for releasing it.