Hot Koehls

The more you know, the more you don’t know

This content is a little crusty, having been with me through 3 separate platform changes. Formatting may be rough, and I am slightly less stupid today than when I wrote it.
22 Nov 2008

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…

function twitter_status($twitter_id, $hyperlinks = true) {

  $c = curl_init();

  curl_setopt($c, CURLOPT_URL, "$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:

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 `user_timeline()` 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.
status as $key => $status) {

* text; ?>

* more...

If you want to see this code in action, just check out the front page of Fwd:Vault, my new full-time startup. While you're checking out the code in action, why don't you follow along with me @fwdvault?
**Build a slick Twitter feed on your site**
  1. Display Twitter updates on your website * Calculate dates and times in different timezones (translate Twitter timestamps) * Parse URL’s in text, create links * **New** Download and store Twitter posts in a MySQL table

comments powered by Disqus