News Archive
PhpRiot Newsletter
Your Email Address:

More information

How to Create Your Own Twitter Widget in PHP, Part 1

Note: This article was originally published at Planet PHP on 5 January 2011.
Planet PHP

Twitter status widgets are 10 a penny, so why create your own? Because you can! Your own widget will always be more customizable than any off-the-shelf solution and you'll be the envy of your peers. We've also been asked by several readers for articles about the topic and it's a great introduction to PHP, REST APIs, JSON, regular expressions and Object Orientated Programming.

The challenge

This is a 3-part tutorial based on source code you can download here. Our objectives are to:

  1. Interrogate the Twitter API and fetch any number of status updates for an individual user.
  2. Apply the data to a configurable html template and convert URLs, @id and #hashtags to proper links.
  3. Format dates into a friendlier format, e.g. posted 10 minutes ago, yesterday, 2 weeks ago, etc.
  4. Cache the widget html so the fetching process is not required during every page load.
  5. Make the resulting widget work in all browsers - yes, that includes IE6!

The Twitter API

Twitter provides a REST API. The following URL fetches the last N tweets posted by a specific user:


  • user_id is the Twitter user name
  • format is either json, xml, rss or atom
  • N is the number of status updates to return

Therefore, the following URL returns Sitepoint's last 10 updates in XML format:


The future of XML is a hot discussion topic, especially following Twitter's decision to drop the format from their Streaming API in favor of JSON. I won't get into that debate here, but my original intention had been to use the XML feed and transform it to html using XSLT. That became a little too convoluted - primarily because links within a tweet must be resolved using regular expressions. That would have been possible using XSLT 2.0 but, unfortunately, PHP's libxslt only supports XSLT 1.0.

I therefore choose JSON; it's a less-verbose format and can be decoded by PHP's json_decode() function.

The TwitterStatus class

The widget functionality is wrapped in a single class named TwitterStatus (refer to the twitter/twitterstatus.php file in the download). Seven public and one private property is defined:

class TwitterStatus { public $ID; // twitter user name public $Count; // tweets to fetch public $WidgetTemplate; // widget template public $TweetTemplate; // template for each tweet public $ParseLinks; // parse links in Twitter status public $DateFormat; // PHP or "friendly" dates public $CacheFor; // number of seconds to cache feed private $cache; // location of cache files note: Public and Private?

If you're new to Object-Orientated Programming, a public property (variable) or method (function) can be accessed from outside the class, i.e. we can set and examine the $ID value from any code no matter where it resides.

A private property or method has local scope and only be used within the class itself. The $cache property can be set and examined within the TwitterStatus class, but it's not visible elsewhere.

PHP also provides protected properties and methods. We're not using those here, but protected members are visible within the class and to inherited and parent classes.

We now require a constructor function which runs when a new TwitterStatus object is created. Our constructor simply sets each property to a default value:

// constructor public function __construct($id = null, $count = 0) { // constants $this-cache = __DIR__ . '/cache/'; // cache location $this-CacheFor = 900; // cache feed for 15 minutes $this-ID = $id; $this-Count = $count; $this-ParseLinks = true; $this-DateFormat = 'friendly'; // default widget template $this-WidgetTemplate = '' . '

Truncated by Planet PHP, read more at the original (another 4690 bytes)