Created
February 17, 2014 00:31
-
-
Save jackdh/9042708 to your computer and use it in GitHub Desktop.
Grab and cache JSON if it is 24 hours old
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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