how to store and retrieve your latest tweet from an XML file

felt twitter bird by basia-hs on deviantART
Photo by ~basia-hs on deviantART

Retrieving and displaying a tweet using the twitter API in real-time as discussed in “how to retrieve and display your latest tweet using PHP” can sometimes produce errors due to twitter’s inability to handle a lot of simultaneous request from multiple users. To deal with that, you can store the tweet in an XML file and retrieve it from there, so you don’t have to rely on twitter’s availability. This way is more efficient and will improve the loading time of the page.

First, you need to create a file named loadtweet.php which will load the latest tweet the usual way, will perform a check to find out if the result is an actual tweet and not an error message by computing the tweet’s length (it must have a maximum length of 140 characters) and will finally save the whole respond to an XML file called tweet.xml:

$twitter_url = 'http://twitter.com/statuses/user_timeline/yourTwitterUserName.xml?count=1';
$buffer = file_get_contents($twitter_url);

$xml = new SimpleXMLElement($buffer);
$status_item = $xml -> status;
$status =  $status_item -> text;
$length = strlen($status);

if ($length < 141)
{
$file = fopen("tweet.xml", "w");
fwrite($file, $buffer);
fclose($file);
}

Your Web Server can be scheduled to execute this file automatically, for example every one minute, using a Cron Job (reffer to your Web Server’s Control Panel or your Hosting Company to find out how to schedule a Cron Job).

To retrieve the tweet from the XML file and display it wherever you want in your page, use the following code:

$xml = simplexml_load_file("tweet.xml");
$status_item = $xml -> status;
$status =  $status_item -> text;
echo $status;
 

test your website in any browser without installing any of them

9 Browsers by jixi2000 on deviantART
Creation by ~jixi2000 on deviantART

All Web Developers/Designers need to test their Websites in several different versions of all the famous browsers to ensure compatibility with each one of them. Sometimes it’s almost impossible to have every version installed on your computer (e.g. multiple versions of Internet Explorer on Vista..).

A great solution to this problem came from Spoon. They created the Browser Sandbox, which allows you to run different versions of all the famous browsers in your computer, without having to install any of them. Only a tiny plugin needs to be installed and then you can run all the supported browsers without any other installation.

The list of browsers that are currently available:

  • Microsoft Internet Explorer 8
  • Microsoft Internet Explorer 7
  • Microsoft Internet Explorer 6
  • Mozilla Firefox 3.5
  • MozillaFirefox 3
  • MozillaFirefox 2
  • Apple Safari 4
  • Apple Safari 3
  • Google Chrome
  • Opera 10
  • Opera 9

The application is free, so why don’t you try it?

 

how to retrieve and display your latest tweet using PHP

Twitter 5 by jasonh1234 on deviantART
Creation by ~jasonh1234 on deviantART

Retrieving your latest tweet from twitter is a very simple process. First, you need to request your latest tweet from twitter using its API and then parse the xml response and select your tweet. It’s much simpler than it sounds!

It’s a useful bit of code to use on your site, and allows people to see whether you’re working, playing online bingo or out on the town without going through the motions of manually accessing your twitter page whilst browsing your site content.

You can request your latest tweet from twitter just by using a simple URL: http://twitter.com/statuses/user_timeline/YourUserName.xml?count=1. By replacing YourUserName with your twitter username, you’ll get an xml file containing your latest tweet (you can request more tweets by changing the “count=1” with the appropriate number of tweets) along with some information about your profile.

Using the following PHP code, you’ll get the content of the xml response to the $buffer variable:

$twitter_url = 'http://twitter.com/statuses/user_timeline/burnmind.xml?count=1';
$buffer = file_get_contents($twitter_url);

Now that you have the xml content, you just need to parse it so you can retrieve your tweet. Using SimpleXML, which is a PHP extension that allows users to easily manipulate/use XML data, the content is converted to a set of nodes. The $status variable holds the “status” node and then the $tweet variable gets the child-node named text, which is your tweet. Finally, the tweet is displayed using the echo command.

$xml = new SimpleXMLElement($buffer);
$status = $xml -> status;
$tweet =  $status -> text;
echo $tweet;

Simple, right?

You can also use an alternative way: how to store and retrieve your latest tweet from an XML file