Simple caching of twitter timelines using PHP

Whilst building this site I decided I wanted to put my recent tweets on the main page, which using cURL and php is a pretty simple task, however the twitter API restricts users to 100 requests per hour and if the limit is exceeded subsequent attempts are blocked until the end of the hour. To get around this I searched around for a library to do the work for me and found many but all seemed to provide overly complex functionality.

This lead me to write my own simple library which gets the most recent tweets from a specified account and caches them to a text file on the server and allows the user to place them in a webage as a simply formatted unordered list of links to the posts. The code for the library and an example are shown below.


<h3>My Recent Twitter Posts:</h3>
<?php include('twittercache.php');
get_feed($username,$limit,$timeout); ?>

If $username is given a vaild twitter username, $limit the number of posts required and $timeout the time to refresh the cache in seconds then the function will print an unordered list as shown above. The library also requires a subdirectory named cache which is writeable to the server.

Note: You will need to refresh the page once before it will work as the first time, as the cache file has not been created. The library does not perform an explicit check for the existence of a cache file and thus the first view creates the file and then on subsequent views uses it. If the permissions of the cache folder are incorrect then the error will reoccur every time.