Skip to content

Instantly share code, notes, and snippets.

@jackdh
Created February 17, 2014 00:31
Show Gist options
  • Save jackdh/9042708 to your computer and use it in GitHub Desktop.
Save jackdh/9042708 to your computer and use it in GitHub Desktop.
Grab and cache JSON if it is 24 hours old
function my_cache_get_event($id){
$url = 'url';
$file = __DIR__.'\cache\user_event_cache_id_'.$id.'.json';
if (time()-filemtime($file) > 24 * 3600) {
// file older than 24 hours
file_put_contents($file, file_get_contents($url));
$json = file_get_contents($file);
return json_decode($json);
} else {
// file younger than 24 hours
file_put_contents($file, file_get_contents($url));
$json = file_get_contents($file);
return json_decode($json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment