Publishing System Settings Logout Login Register
Grabbing and displaying your Twitter status using PHP
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on August 8th, 2007
11098 views
Twitter


This tutorial will be using the SimpleXML functions  available from PHP5 onwards. Although I would suggesting aiming to code to/with PHP5 if you are not already, you can easily modify the code provided here to work with a different XML class.

One of the problems I have encountered during the production of my forthcoming website is how to use the Twitter service on my website. Ultimately I have ended up in a position where I am both able to grab my current status and update it from the site's backend, however in this tutorial we will just be covering how to do the first of these things.

Being a relatively new Twitter user I admit to not knowing much about the service, or more importantly the API. I had seen it done a few times on various websites and as with most things web related I suspected it was able to be done with PHP so I set out investigating the sites API and how I could use it to my advantage.


An example of what we will acheive as seen on http://5thirtyone.com



Upon checking into the Twitter API it turns out it is relatively privative with only feeds being supplied  in XML, JSON or RSS formats.

Here is where I fell down. My first attempt at doing this involved the JSON format, cURL and the JSON functions (again, only available in PHP5) and to give fair credit to myself, this did work. However looking back over what I had made me think that actually half the code I had simply wasn't necessary, and certainly after re-reading the Twitter API page I found this to be correct.

We are going to user the XML feeds available along with your username (screen name). It is also possible to use your user id however since I think it's safe to assume many of you don't change your usernames every few minutes I will use the username to identify you as it is marginally easier.

We are going to be using this feed for the "twitter grab" so to make sure you understand what's going on for the next few steps I would suggest reading this:

user_timeline

Returns the 20 most recent statuses posted in the last 24 hours from the authenticating user.  It's also possible to request another user's timeline via the id parameter below.

URL: http://twitter.com/statuses/user_timeline.format

Formats: xml, json, rss, atom

Parameters:
      
  • id.  Optional.  Specifies the ID or screen name of the user for whom to return the friends_timeline.  Ex: http://twitter.com/statuses/user_timeline/12345.xml or http://twitter.com/statuses/user_timeline/bob.json.
  •   
  • count.  Optional.  Specifies the number of statuses to retrieve.  May not be greater than 20 for performance purposes.  Ex: http://twitter.com/statuses/user_timeline?count=5 

So now that we've got sorted what we are going to need to use, lets get doing it ;)


As mentioned in the previous text the url follows this structure: user_timeline/username.format(arguments). We have already decided we are going to use the XML format and that we want to grab the current status so our format is XML and our only argument is count=1.

For the sake of tidiness, lets stick this in a variable.
$twitter_url = "http://twitter.com/statuses/user_timeline/$username.xml?count=1";


You may notice here I have left the username as a variable. This allows you to expand the code into a function or simply for you to change the username easier in the future.

Visiting this url now will display (if using firefox) the XML structure we are going to need. We will go back to this in a second.

Our next step is to gather the contents of this XML file and buffer it for use by the SimpleXML functions. Initially I myself made the mistake of using cURL for this, however somehow I found the structure was lost when doing this and so have turned to file_get_contents for the sake of both easier to follow and minimal code.

So just take the function, and substitute out variable holding the feed URL into the first argument. We also  need to retain this data for later use so lets stick it in a variable - something descriptive is always good.
$buffer = file_get_contents($twitter_url);


Now we need to go back to our XML structure. Below is a screenshot of mine, yours will be the same but with different data obviously.





From now on we will be using the very basic functions of the SimpleXML library. It is a very powerful and somewhat underused extensions and I would suggesting reading up on it.
http://uk2.php.net/simplexml

We need to first create a new XML string for use by the SimpleXML functions. This is pretty basic and shouldn't require much explaining. If you need to, the php.net/simplexml pages will be able to guide you.

$xml = new SimpleXMLElement($buffer);


Again, the first argument is the XML data. What we need to do now is take both the Node and Element we need to take data from. This is the same for everyone but I think it's important you know how:



Taking this data with SimpleXML is, as best as I can describe, like working with nested classes. It's best to see an example:
$status_item = $xml -> status;


See the $xml -> status? Remember status is the name of our node. So now we are working within that node, we can take the text from the text element by doing this:

$status = $status_item -> text;


And believe it or not, you are now finished!

The complete code:
<?php
$twitter_url = "http://twitter.com/statuses/user_timeline/$username.xml?count=1";
   
$buffer = file_get_contents($twitter_url);
$xml = new SimpleXMLElement($buffer);
   
$status_item = $xml -> status;
$status =  $status_item -> text;

// echo $status;
?>


[MY_P2L_AD]

Hopefully this tutorial has provided you with something useful and a small insight into SimpleXML.

All comments are appreciated and as ever if you post a topic in the forums someone is always around to help you out should you have any problems.


One thing you should not while playing around with this is that as Twitter has grown progressively larger, it has slowed down. The code I have walked you through here should not be implemented as it will slow your site down quite dramatically as any request to an outside page will. What I have done, and something you may wish to consider, is caching the status and either running a cron job to update it every hour or so or update it manually yourself. Either way you will see a monumental load time decrease in comparison to requesting the data from Twitter.

A Final Message...

The Twitter API, while being slightly privative in comparison to others, is actually quite powerful. Even in something as simple as the XML feed used here, there is a welth of information provided. Certainly something worth playing about with if you are an avid Twitter user.

In possible following tutorials I may cover how to perform various other actions such as updating your twitter status from your own website pages, and other interactions.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Matthew.

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top