Caching API responses with PHP

Posted on April 2, 2011 by Phil   13 comments

Most websites have some kind of widget that fetches data from an external RESTful API provided by services such as Twitter, Flickr and Facebook to name just a few. These APIs often limit the amount of hits any one Domain/IP can make and if you go over your rate limit then pow, no data for you and an empty widget. This PHP class gives you a mechanism to cache your API responses locally and only update them on a set time interval so you always stay within the rate limits for each API call you make from your site.

API_cache.php

The class accepts a URI for your API call, a filename to write to (if this is not in the same directory as the class you will need to make sure that directory exists), and a time period for how long you wish to cache the response for. Each time the cache contents are requested the object checks to see if the file is out of date before updating it and returning the files contents. Of course if the file is up to date it just return its contents and does not make the API call. This avoids going over the API rate limit and also reduces unnecessary network traffic giving a better experience for your visitors in general. Below is the code for the API_cache class.

Using the API_cache class effectively

Here is an example usage of the class taken from the demo. The flexibility of this class means you could have cached your API response in any format, it doesn’t need to be JSON this is just my preferred option as I generally use Ajax to build the widgets once the site content has loaded. All you need to do is build up your API URI, tell it which file to write to and set how often you want to update it – instantiate your cache object and ask it for your API response. You could even use it to cache other external data such as atom feeds pulled in from other sites.

The class also has a getter for the cache expiry date and time–returned in HTTP header format–that you can use to build up your response headers and let the client know how long to cache the response for.

Related Posts

No related posts.

13 Responses So Far

  1. Simoney April 25, 2011 at 6:53 pm

    Thank you so much for the scricpt, that is what I was looking for. I truly appreciate it.

  2. tonetone May 5, 2011 at 3:52 pm

    Fantastic script, just what I was looking for. Much easier to control user refreshes at server side!

  3. Slotspiele October 25, 2011 at 6:46 pm

    excellenta lismopro mi ventelh te vitag arias fúposondu. irans te ssendo amamia nos ignistamo o onstruis pefegela mucabi bien.

  4. Pingback: Http Header Format | HTTP Get

  5. Richard Freeman February 29, 2012 at 7:30 pm

    Excellent script, thanks for your nice work. Could you tell me how I could use this for multiple APIs?

    Thanks for your help :)

  6. Richard Freeman February 29, 2012 at 7:35 pm

    Oh never mind, dumb question. haha :D

  7. Pingback: CORS with Ajax (Cross origin resource sharing) | Phil Parsons

  8. Adrian July 3, 2012 at 9:56 am

    Great script! Found this very useful. Thank you :)

  9. freebird August 21, 2012 at 11:54 am

    I want to know your further steps to use it. If i want to show the response on page then should I need to hit json file or php file for getting the data ? If I need to hit php file then what will be the significance of JSON LOCAL file ??

    Thanks

  10. Phil August 21, 2012 at 12:03 pm

    You make a request to the php file. The class will then decide whether to refresh the local JSON file from the web service or just return it’s contents based on the expiry time you set for that file.

  11. felix September 27, 2012 at 11:55 pm

    amazing, even a php dumbass like me managed to use this – and it works! thanks man, really appreciate you sharing this

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recommended reading

  • Ben Nadel Articles on “obsessively thorough web development” with Coldfusion and jQuery
  • Christian Heimann's blog – Wait till I come!! Articles on web development
  • David Walsh Articles around PHP, CSS, MooTools, jQuery and just about everything else
  • John Resig Javascript programmer and the creator of the jQuery library
  • Mary Lou (Codrops) Stunning designs with lots of cool jQuery effects

Photostream

  • Loading photostream from Flickr...