articles menu
General Computing- POP vs IMAP What is the difference
- How to Remove Browser Proxy Settings
Web Design
- PHP function to display latest Twitter tweet
- PHP function to summerise content in whole words substr
- Webmasters Reference Links
- Mod rewrite Basics for SEO
- Useful 404 error pages
Articles - PHP function to display latest Twitter tweet
We decided to add our latest Twitter tweet to our home page as we like to micro blog. The question was how do we do it?
Twitter has various methods for data retrieval including, XML, RSS and their own API feeds that we could use. After looking at them all we decided to use the XML feed as it seemed the quickest. This is our xml feed url:
http://twitter.com/statuses/user_timeline/21885742.xml?count=1
To get yours click the RSS link on your twitter page to get your Twitter Id, your Id is the filename (without the .rss extention). Then substitute our Id from the link above with your own.
We then had to decide how we were going to extract the required data, as we just wanted the comment we decided to utalise cURL. Info on cURL can be found here http://php.net/manual/en/book.curl.php. If we had needed to handle more data we would probably have gone with XML_DOM.
The function:
function get_twitter_status($twitterId) {
$ci = curl_init();
curl_setopt($ci, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitterId.xml?count=1");
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ci);
curl_close($ci);
preg_match('/<text>(.*)<\/text>/', $data, $d);
$status = htmlentities($d[1]);
return($status);
}
Its as simple as that really, to use it on a page use the following PHP code:
echo get_twitter_status(put your twitter id here);
Hope this helps someone!
Created on Jan 8, 2010

