Really Simple PHP CURL and Cache of JSON Result

When using PHP CURL with a third party API, I’ve found keeping the JSON results in a local cache is easier than constantly querying the service – especially when the API is slow or has query restrictions.

Here’s a great snippet I save in my tool belt and use quite often:

If the API is really slow (I’ve encountered two myself) I suggest splitting the snippets CURL+Cache section and putting it into it’s own file, from there create a CRON command to execute the independent CURL+Cache file. That’ll separate the slow API from your PHP app, and your JSON data will still be up to date in the background.


  1. Thanks David, for the simple solution for a complex issue.. 🙂

  2. This could not use in flight or hotel search data. there request and response data would be change many time. what should we do there

  3. Hi! I’m curious about how you would handle LOTS of different API calls? If there are 14,000 calls with different responses, would you store these all as separare files? Store the results in one file and loop through them to see if the result is cached?

Leave a Reply