I recently wanted to display my latest tweets on my website since I am trying to use Twitter more regularly.  Until I was able to do this, I knew nothing about Twitter or its API's and I didn't really feel like researching it much either.  I wanted a quick solution and a simple google search lead me to Nick Heer's blog post Display Your Latest Tweet With PHP.  Nick cites that he pulled the code from Smashing Magazine, but Smashing Magazine actually got it from Ryan Barr at Script Playground.  Now that I've given credit where credit is due, I can tell you that Ryan Barr's code actually only provides you with your last tweet.  I wanted to display my last 5 tweets so I had to modify his code, but before we move onto this, here's the updated code that we'll be working from as modified by Nick Heer:

$username = "e_d_s_a_t_a";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";

function parse_feed($feed) {
    $stepOne = explode("<content type=\"html\">", $feed);
    $stepTwo = explode("</content>", $stepOne[1]);
    $tweet = $stepTwo[0];
    $tweet = htmlspecialchars_decode($tweet, ENT_QUOTES);
    return $tweet;
}

$twitterFeed = file_get_contents($feed);
echo('"'.parse_feed($twitterFeed).'"');

What's Going On In The Code Above?

The code above uses PHP's file_get_contents function to grab an RSS ATOM feed for a particular username (in the code above: e_d_s_a_t_a) from search.twitter.com. It takes the HTML contents (placed in the $twitterFeed variable) and extracts the last tweet from the feed using the parse_feed function. Why only the last one? If you notice in the feed URL there is a parameter (rpp) set to 1. You can set that parameter to any number of tweets you want displayed.

Let's Modify The Code To Display The Last 5 Tweets

If you were to look at the HTML contents of the twitter feed, you would understand better what the parse_feed function is doing in the code above. It is exploding the feed into parts and placing these parts into an array. It then takes the 2nd element (at position 1) and explodes it into parts. The first element in the resulting array is then the content of the tweet. Let's modify the parse_feed function to return multiple tweets:

function parse_feed($feed) {
    $tweets = array();
    $data = explode("<content type=\"html\">", $feed);
		
    for($i = 1; $i < count($data); $i++) {
	$tweet = explode("</content>", $data[$i]);
	array_push($tweets, htmlspecialchars_decode($tweet[0], ENT_QUOTES));
    }
    
    return $tweets;
}

We're doing, almost, the same thing before, except we are looping through all the items in the first array and extracting the contents of each tweet. The contents of each tweet is placed in the $tweets array and returned. We can then output the resulting tweets any way we want, but before you do, make sure you modify the (rpp) parameter in the feed and set it to however many tweets you want to display (I've set mine to 5). Here's the updated code:

function parse_feed($feed) {
    $tweets = array();
    $data = explode("<content type=\"html\">", $feed);
		
    for($i = 1; $i < count($data); $i++) {
	$tweet = explode("</content>", $data[$i]);
	array_push($tweets, htmlspecialchars_decode($tweet[0], ENT_QUOTES));
    }
    
    return $tweets;
}

$username = "e_d_s_a_t_a";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=5";
		
$twitterFeed = file_get_contents($feed);
$tweets = parse_feed($twitterFeed);

foreach($tweets as $tweet) {
    echo "<p>$tweet</p>";
}

Now you too can display your most recent tweets on your site and use CSS to style them however you wish!