Before you waste any of your time playing with this code you should know that this blog post discusses my TwitterCacher library which uses basic HTTP authentication to communicate with the Twitter API. Twitter no longer supports basic HTTP authentication as of August 31, 2010. You need to use OAuth instead to authenticate with the Twitter API. More details on that can be found here and here. Use the code and tips discussed in this blog post at your own risk.
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.
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 post helpful, or at least, interesting?