Get Your Most Recent Twitter Posts Using PHP with RSS

We all know how annoying it is to not have any customization with the ways Twitter provides to show your latest Tweet. We all also know how annoying it is that your server lags out if the Twitter service is down when using the ways they provide. The obvious alternative method is to use the Twitter API to pull out your latest tweet, but that could cause more problems than it fixes, especially if you are requesting information from the server too much.  So, what’s the alternative? RSS.  Twitter generates a dynamic feed that allows parameters to be set to limit the results. (Documentation on Formats from Twitter.) For example, here is a feed showing results for my account: Notice how my username can be seen from the URL. Problem here is we are only looking for the latest tweet, so we will use the rpp parameter Twitter has made for us to limit our results to a single tweet (Click here to retrieve more than one tweet.): our latest tweet. Pretty boring, right? That’s what I expected and exactly why I have developed the following script to make it easy for everyone to implement on the fly:

<!--?php <br ?-->/*  Copyright 2010  Ali Han */

// Your twitter username.
$username = "TwitterUsername";

//Prefix - some text you want displayed before your latest tweet. (HTML  is OK, but be sure to escape quotes with backslashes: for example  href="link.html")
$prefix = "";

// Suffix - some text you want display after your latest tweet. (Same  rules as the prefix.)
$suffix = "";

$feed = "" . $username .  "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("", $feed);
$stepTwo = explode("", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("}

$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) .  stripslashes($suffix);

You will need to update the $username variable to hold your Twitter username. You will need to be running PHP on the server and the file extension must be .php (unless you are using .htaccess to treat .html files as PHP). Other than that, wherever you place this code on your page is where the information is output. (Have basic PHP knowledge? Try moving this script to a header file and populating a variable that gets displayed later on.) As this is part one of a four part series there are a few things to understand:

  • The script above only pulls the latest tweet from the RSS, no timestamps or other information. (Part two grabs more.)
  • The script is pulling from a file on the Twitter servers, so you are still depending on that file being active.
  • As this script is pulling from an outside file, you still might experience some delay. Though the delay exists, it isn’t much a difference from the Javascript method that is currently available.

Interested in taking the script further? How about pulling more tweets and having more customization options? Visit Part Two of the tutorial now… Want to skip ahead and get even further? Try Part Three where you can pull multiple tweets from multiple Twitter users!

( Ali Han, )

Get Your Most Recent Twitter Posts Using PHP with RSS WordPress Theme
Reviewed by Ali Han on May 18th 2011
Rating: 5