Display Twitter updates on your website

Update: I’ve added a new chunk of code that will download and store your Twitter posts in a database, allowing you to do whatever the heck you want with them. After you’ve finished reading this, be sure to check that out as well. I am not a fan of social networking or so-called lifestreaming. I think it’s a BS excuse to fiddle on your computer more. Instead of telling everyone where you are and what you’re doing, go out and meet some friends for a drink. However I did find a practical use for Twitter in a recent issue of php|architect (Twitter as a Development Tool by Sam McCallum). The article discussed using Twitter as an automated logger, where a program would make posts to a Twitter account based on system actions (i.e. log in/out, create accounts, etc.). I decided to turn the idea around a bit and use Twitter as an activity log to chronicle my development work on a new project. Think SVN log comments without the repository. The site itself is currently a simple placeholder page, so Twitter updates make an easy way to keep a website fresh while building out the service that will eventually reside there. It also engages the users that wind up looking at the site, letting them know that it might be something of interest to them. That’s to say nothing of any SEO or attention-grabbing effects that may result from having a Twitter stream. Given the rabidity surrounding said scoial networking silliness, I thought that finding a suitable plug ‘n play solution to this would be easy. Surprisingly (or perhaps unsurprisingly) many of the Twitter scripts I found were plain garbage. The following code was put together by sifting through what I found and putting the best working bits together. So if this sounds interesting, or if you were also frustrated with the plethora of crappy Twitter code, here’s how you can easily display your Twitter updates on any site using PHP. First, grab this function…lang="php">function twitter_status($twitter_id, $hyperlinks = true) {

$c = curl_init();

curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml");

curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);

curl_setopt($c, CURLOPT_TIMEOUT, 5);

$response = curl_exec($c);

$responseInfo = curl_getinfo($c);


if (intval($responseInfo['http_code']) == 200) {

if (class_exists('SimpleXMLElement')) {

  $xml = new SimpleXMLElement($response);

  return $xml;

} else {

  return $response;


} else {

return false;


} “`

I’m not going to discuss the various cURL options here or how Twitter uses cURL, as its outside the scope of our discussion here. If you’re lost or curious, you can read up on the cURL library, cURL in PHP, and/or the Twitter API. As its name implies, twitter_status() will connect to Twitter and grab the timeline for the Twitter account identified by the $twitter_id. The $twitter_id is a unique number assigned to every Twitter account. You can find yours by visiting your profile page and examining the RSS link at the bottom left of the page. The URL will look like this: http://twitter.com/statuses/user_timeline/12345678.rss

That 8-digit number at the end is your ID. Grab it and pass it as the lone argument to twitter_status(). Note that, as long as your Twitter profile is public, you do not need to pass any credentials to retrieve a user timeline. The API makes this information available to anyone, anywhere. There are more options that can be accessed through the <a href="http://apiwiki.twitter.com/REST+API+Documentation#usertimeline">user_timeline()</a> function, if you’re curious. The next step is to actually use the returned data, which comes in one of two forms: a SimpleXML object, or a raw XML document. SimpleXML is preferred because it’s a PHP object, and allows you access to all the usual object manipulation. Very easy. SimpleXML was added to PHP starting with version 5. The PHP manual has all the necessary details on SimpleXML. The following code example assumes you’re using SimpleXML. Here I am taking the first five results and putting them in an HTML list. I’ll include a link to view the profile, as well as an error message in case Twitter is suffering from one of its famous fail-whale spasms.lang="html"> <?php

if ($twitter_xml = twitter_status('12345678')) {

foreach ($twitter_xml->status as $key => $status) {

?> * <?php echo $status->text; ?>



if ($i == 5) break;


?> * more…


} else {

echo 'Sorry, Twitter seems to be unavailable at the moment…again…’;



If you want to see this code in action, just check out the front page of <a href="http://fwdvault.com">Fwd:Vault</a>, my new full-time startup. While you're checking out the code in action, why don't you follow along with me <a href="http://twitter.com/fwdvault">@fwdvault</a>?
**Build a slick Twitter feed on your site**
<li class="green bold">Display Twitter updates on your website
* <a href="/2008/11/easily-calculate-dates-and-times-in-different-timezones">Calculate dates and times in different timezones</a> (translate Twitter timestamps)
* <a href="/2008/12/parse-urls-in-text-create-links">Parse URL&rsquo;s in text, create links</a>
* **New** <a href="/2009/08/archive-entire-twitter-timeline/">Download and store Twitter posts in a MySQL table</a>