Using the Twitter API, Cache Your 'Tweets' in PHP (TwitterCacher)

| 1 TrackBack
WARNING:

Before you spend any of your time playing with this code, you should know that Twitter plans to stop supporting HTTP Basic Authentication on June 30, 2010.  This means that starting on June 30th, the code discussed here in this blog post will stop working.  To use Twitter's API after June 30th, your application must support OAuth.  TwitterCacher does not support OAuth and I have no plans to make it OAuth compatible.  That said, you will probably find this OAuth compatible Twitter PHP library much more useful. You might also find this writeup on Using one access token with OAuth useful.


I spent a few cycles this afternoon integrating my Twitter timeline with my Movable Type blogging software.  Surprisingly, things worked out great with no issues in the first version.  The Twitter API is a well-written REST service that made it incredibly easy to combine my lifestream with my Tweets.  BTW, I hate the word "Tweet" but that's the terminology everyone in Twitter land seems use so I will too.

To start, I snagged the php-twitter library from Google Code.  The only php-twitter functions I really cared about were process() and userTimeline(), which use libcurl to read your Tweets in JSON or XML format.  Once I had the code I wanted, I created my own PHP class named TwitterCacher to handle all of the API calls and response caching.  The idea behind TwitterCacher is to work around a rate limiting problem of the Twitter API.  Twitter limits the number of API requests you can make in an hour or so, so if you use the Twitter API you have to be conscious of this limitation (otherwise, Twitter will blacklist you).  As a result, the developer using the Twitter API has to implement some type of caching mechanism to prevent hitting Twitter too often.  To solve this caching issue, my TwitterCacher saves the JSON or XML response from the Twitter API to a flat-file on your web-server.
twitter-cacher-screenshot.jpgTwitterCacher will only hit the Twitter API for new Tweet data if the JSON or XML stored in the flat-file is older than 30-minutes.  The flat-file has the format twitterID.format where twitterID is your Twitter user name and format is either "json" or "xml", depending on the format you've requested.  This cache file is stored inside of the same directory as the TwitterCacher class on the web-server.

My TwitterCacher PHP library can be found here.  I'm also using Gagawa PHP to build the HTML which is sent to the browser, and this JSON PHP library to convert the JSON response from the Twitter API into something more useful.


Mark, why aren't you using a database to cache your Tweets?

I know, I know.  Hardcore MySQL junkies would argue that I should use a database to cache the JSON instead of a flat-file.  Dude, come on, I'm not going to setup an entire database/table just for a single line of JSON.  Databases are a great solution for many data storage problems, but this is definitely not one of them.


FOLLOWUP (USAGE EXAMPLE) 4/6/09:

I realized that I posted the TwitterCacher library, but I didn't provide any examples that explain how to use it.  So, here's an example that shows you how to use TwitterCacher:

<?php

require_once("JSON.php");
require_once("TwitterCacher.php");

$twEmail = "you@example.com";
$twPass = "password";

$json = new Services_JSON();
$tc = new TwitterCacher($twEmail,$twPass);

// I do this just to be a nice guy. So Twitter knows who we are.
$tc->setUserAgent("Mozilla/5.0 (compatible; TwitterCacher/1.0;)");

// Read the timeline from the cache, or from Twitter.
$timeline = $json->decode( $tc->getUserTimeline() );

foreach( $timeline as $tweet ) {

// Get the Tweet itself.
$text = $tweet->text;

// Twitter uses GMT+0 but I convert it to my local time.
$date = date('M j, Y @ h:i A', strtotime($tweet->created_at));

echo $text . "<br />" . $date . "<hr />";

}

?>

You can find a more complete example here (TwitterCacherExample.phps).
Hope this helps.


ANOTHER REAL LIFE EXAMPLE (TWITTERBADGE) 5/21/09:

I recently received a nice Tweet from Kien Tran regarding his custom implementation of TwitterCacher, known as twitterbadge.  Kien greatly extended the functionality of TwitterCacher to do some pretty neat stuff.  He also made great use of Gagawa PHP, an HTML generation engine I wrote in PHP.  Thanks, Kien!

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

Send Mark a Direct Message

If you'd like to send me a direct message, please do so below. However, I do not publicly post comments or messages submitted directly to me. So, if you're going to try to SPAM me, or my blog, you're pretty much wasting your time.

400 characters remaining

Error

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer, a casual entrepreneur, and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

1 TrackBack

About This Site from Mark S. Kolich on March 18, 2010 11:57 PM

I'm Mark Kolich, and this is my personal weblog.  I usually write about my technical interests, which mostly relate to client and server-side web technologies on various platforms and in numerous programming languages.  I enjoy solving tough ... Read More

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on March 2, 2009 8:20 PM.

Finally Joined Twitter (twitter.com/markkolich) was the previous entry in this blog.

Filename Case Sensitivity: Another (Annoying?) Linux vs. Windows Difference is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.