Hi,
I am caching a json result from an API and saving it as a file on my server.
I have the following code that checks this files age and replaces it with a newer version if its too old.
It works great locally but is not allowed on my host due to security risks. How can I best replace this file_get_contents method with a file_get_contents_curl one? Is file_get_contents_curl the way to go? Alternatively - if there is a better way of doing this I am all ears
if (time()-filemtime($url) > 1200) { // file older than 20 minutes
$json = file_get_contents("http://service-api-url/json/", true); //getting the file content
$decode = json_decode($json, true); //getting the file content as array
$fp = fopen('data/myjson.json', 'w');
fwrite($fp, json_encode($decode));
fclose($fp);
echo $json;
} else {
// file younger than 20 minutes
$json = file_get_contents($url);
// echo the JSON
echo $json;
}
Thanks!