Live Status Updates With PHP

If you have a personal website, and you use Twitter, there’s a chance you already have a Twitter widget embedded in your site. Most people will opt for the default Twitter-provided (Javascript-based) widget, which does its job well, is relatively customizable, and is very easy to implement.

However, some people (like myself) are stubborn and would rather come up with their own custom solution that allows for complete customization. I wrote my own basic Twitter feed widget in PHP for the latest rendition of my homepage.

This custom widget has complete PHP and CSS-based control over how you display the latest tweet. It fits the content into the exact dimensions you want, with a bare minimum of content, so you can style it exactly how you want. I’ve also set it up to automatically grab the geolocation data from the tweet, or to fall back to the client used to tweet if there’s no geotag. You can also (by default) filter the latest tweets selectively – I’ve chosen to omit @replies and re-tweets, as personally, I’d rather have my own original tweets displayed prominently on my site.

I’ve made this widget open-source – download it from github if you’d like. You’re free to use it wherever you want. There’s only one PHP file to import, and only one function to call. It makes use of PHP’s built-in SimpleXML module, to parse through Twitter’s live RSS feed of your own tweets and extract the first one that’s not an @reply or retweet.

Some future plans for the code:

  • Allow for local caching to reduce fetching from Twitter’s rss feed
  • Find some way to allow for private twitter feeds (using their API perhaps?)
  • Offer a more flexible div structure
  • Add the Javascript necessary to load the widget with an Ajax request

In addition, if anybody uses Last.fm and is interested in having a similar widget for last song scrobbled, I’ve created and open-sourced such a widget, also available for download from github. However, this widget requires an API key, so you’ll need to sign up for an API account if you’d like to use it.

If you’ve got any comments or suggestions, feel free to leave a comment below.