Display your last Tweet from Twitter.com

Are you a big Twitter user, and would like to integrate your account into your site? Here is the code to display your last Tweet using the .xml file that is provided by Twitter.

There is only one thing that needs to be changed out. which is the XML file address, you need to input your Twitter ID #. You can get this from clicking on the RSS feed at the bottom of your Twitter Main Page, and extracting this from the URL.

<?php
//Put XML Contents Into Array (PHP.net Function)
function xml2assoc($xml) {
$tree = null;
while($xml->read())
switch ($xml->nodeType) {
case XMLReader::END_ELEMENT: return $tree;
case XMLReader::ELEMENT:
$node = array(‘tag’ => $xml->name, ‘value’ => $xml->isEmptyElement ? ” : xml2assoc($xml));
if($xml->hasAttributes)
while($xml->moveToNextAttribute())
$node[‘attributes’][$xml->name] = $xml->value;
$tree[] = $node;
break;
case XMLReader::TEXT:
case XMLReader::CDATA:
$tree .= $xml->value;
}
return $tree;
}
//Open XML Reading Object
$xml = new XMLReader();
//Open XML File (Replace {id} with your Twitter ID)
$xml->open(‘http://twitter.com/statuses/user_timeline/{id}.xml’);
//Put Document in Arrays
$assoc = xml2assoc($xml);
//Close XML Object
$xml->close();
//Store Result
$lastTweet = $assoc[0][‘value’][0][‘value’][2][‘value’];
//Print Result
echo $lastTweet;
?>
Hope this helps you in your web development. As always, please feel free to leave any comments or suggestions.
error

Enjoy this blog? Please spread the word :)