Easily Getting your last Tweet through PHP5

Have you ever wanted to get your last tweet easily through PHP?
After a little research I think I’ve come up with a very quick and simple way.

How it works:
Using PHP’s DOMDocument method we can quickly parse the rss feed. From there we grab the “description” node from the first item and BOOM we have our last Tweet.

How to do it:
Get your rss feed from Twitter. Parse the feed using the DOMDocument class in PHP.

How it’s done:
Simple insert the following code into your php document and replace the user id with your Twitter id.

$user_rss = 'http://twitter.com/statuses/user_timeline/(your_user_id).rss';
$dom_doc = new DOMDocument();

// grab all 'item' nodes
$node_list = $dom_doc->getElementsByTagName('item');

// grab the first description in the first item node
$desc = $node_list->item(0)->$item->getElementsByTagName('description')->item(0)->nodeValue;

// print the string after the colon
echo substr($desc,strpos($desc, ":")+1);

This will NOT work in PHP4. DOMDocument is limited to PHP5.

This entry was posted in tutorials. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>