twitter facebook dribbble email

Twitter plugin

Careful! This post is looking a little old and could be inaccurate in many, many ways

Earlier in the year I was faced with an issue of integrating a Twitter feed into a website, problem was the website wasn’t using WordPress. At the time it appeared like every Twitter plugin available was built to integrate with the popular blogging platform. At the time I was able to hash together a working PHP function but over time problems kept coming up resulting in much needed improvements. Finally I think I have something that’s fairly indestructable.

Caching

This was one of the first updates made to the function. After first integrating the function it frequently displayed ‘No public twitter messages’ rather than the latest tweet. The root cause was that the function made a new request to the Twitter servers on each page view, this would easily max out our request allowance.

Caching, for those unaware of it, is a way of saving frequently requested information to either reduce the number of requests for such information or to speed up the retrieval of information or files (or at least that’s how I see it).

In this function the first thing we do is to check if the cached file exists and how old it is. Here if the file is over 300 seconds (5 minutes) old or if the file doesn’t exist at all the function will request the information from Twitter, if not then it will use the cache. This reduces a potential endless number of requests to only 12 per hour, well below Twitter limits.

Getting the information from Twitter

This is another area of the function that underwent a few changes, the most recent of which was to address the DDOS attack on Twitter recently. With Twitter’s servers straining under the weight so did the websites using this function. The solution was to ensure the function would time out so that if Twitter was running slow or was down altogether the function would stop and return to the cache.

Using cURL seemed the best way to retrieve the information required that utilised a timeout attribute, here it times out after 4 seconds. Once the cURL is executed the $output variable contains a XML document.

Outputting the tweet in the format of our choosing

With the tweet contained within the format of an XML document using the DOMDocument function appeared the best way to extract the information as it allows you to specify the tag names within the XML to extract the information required.

This section of the function checks firstly that the document will load, if it does then it continues to extract the relevant information into an array and then output the information in a predetermined format. At the same time the function also takes this information and updates the cached file, ensuring there’s an up to date version of the file to be used for the next few minutes.

Using the cache

The last peice of the function is actually using the cache. The cache is primarily used to reduce the number of request made to Twitter, but it also serves as a fall back in case any issues arise elsewhere in the function. And of course even if the cache is unavailable there’s a simple response just to make sure there’s always something returned by the function.

This part of the function gets the contents of the file and splits the information using a ‘hopefully’ unique character string ( :: ) used when creating the cache to keep all the information separate. The $account and $displayname are parameters of the function.

The function in full

Stuff still to do

There’s always ways to improve a function, here’s a few I’ve already identified:

  • Allow for caching of multiple users so that the function can be used multiple times on the one website
  • Search and create links within tweets where necessary, this includes @mentions and #hashtags if possible
  • Create method for better definitions of timestamps

Hopefully if I get any changes done to the function I’ll upload it to my blog again, in the meantime feel free to play with it yourself, I’m sure more hard core developers than me can see loads more ways of improving the code.