I'm using the darksky api to get weather data for a display board website.
The free version is limited to 1000 api calls per month.
I have a lot of devices (iPads) displaying this information in the building and refreshing every minute or so to update weather and the message of the hour.
I think that caching the weather into a file and reading it when the refresh time is over is a solution to my problem.
Is it the best solution?
And is this the best implementation of this solution
function getWeather(){
//compares the time of the saved weather at each call of the method
$last_modif = filemtime("weather.json");
//if the weather is 10 minutes old, renew it by calling the api
if(time() - $last_modif > WEATHER_REFRESH_TIME){
//echo "call api";
$file = fopen("weather.json", "w");
$request = 'https://api.darksky.net/forecast/MY_KEY/LAT,LNG?lang=fr&units=si&exclude=minutely,flags';
$data = file_get_contents($request);
fwrite($file,$data);
fclose($file);
}
$file = fopen("weather.json", "r");
$weather = fread($file,filesize("weather.json"));
fclose($file);
return json_decode($weather);
}
Should i write the data in a database instead or is there any other way to store the weather?
What are the best practices in this case ?
file get contetns
then why not use it below instead of fopen/fread/fclose. You could also shorten the code by using file_put_contents. \$\endgroup\$