PHP Cache weather in a file
Whether you stick with a flat file or a database depends on how you're using the data. It looks you you're only caching the latest API response for one specific query and not any historical data or data for other queries. If you have no desire to use those other data, caching to a file as you're doing is the easiest solution.

If you want to use other data at some point (maybe for multiple offices or historical graphs), a database would be far more scalable and manageable.



Spinning up a basic MySQL DB on your server would be fairly painless.


Install MySQL, if needed: https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-14-04
(Optional) Install PHPMyAdmin, if you're not comfortable running MySQL commands over the terminal: https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-16-04
Create the weather database and user.
Create whatever tables you need for caching the data. This should include either a column next to the data saying when it was cached or a table that records the time of the api queries.
Use some basic PDO to connect to the database and query it: http://coursesweb.net/php-mysql/pdo-introduction-connection-database




With either scheme, if users are hitting your service frequently enough, you can continue with your current scheme of requerying the API as you serve the page, after the data invalidation interval.

If you want to prevent any slow-downs for users or if they're not hitting it regularly enough, you could create a cron job to run your update script, regardless of where you're dumping the data.



As for your current script, I wrestled a bear once is correct. You should try to avoid opening and closing the same file repeatedly. You might try refactoring to this:

function getWeather()
{
    $last_modif = filemtime('weather.json');

    $response = '';

    if(time() - $last_modif &gt; WEATHER_REFRESH_TIME)
    {
        $response = file_get_contents('https://api.darksky.net/forecast/MY_KEY/LAT,LNG?lang=fr&amp;units=si&amp;exclude=minutely,flags');
        file_put_contents('weather.json', $response);
    }
    else
    {
        $response = file_get_contents('weather.json');
    }

    return json_decode($response);
}