skip to Main Content

I have code where I get weather forecast data from the Wunderground API. The problem is that I put the script on the website yesterday and since then it has been displayed constantly the same, even though the data in the JSON file from Wudnerground is already updated when I view this file.

Something is wrong, but I don’t know what… Below is my php code.

$json= file_get_contents('https://api.weather.com/v3/wx/forecast/daily/5day?geocode=51.42,17.26&format=json&units=m&language=pl-PL&apiKey=MY API KEY');

$data = json_decode($json);
$day = $data->dayOfWeek;

$maxTemp = $data->temperatureMax;
$minTemp = $data->temperatureMin;
$icon = $data->daypart[0]->iconCode;
$deszcz = $data->daypart[0]->precipChance;
$windDir = $data->daypart[0]->windDirectionCardinal;
$windSpeed = $data->daypart[0]->windSpeed;
$UV = $data->daypart[0]->uvIndex;
$txt = $data->daypart[0]->wxPhraseLong;

echo "<table style='width: 100%;'>";
echo "<tr><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:18px; color:#c9c9c9'>$day[1]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:18px; color:#c9c9c9'>$day[2]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:18px; color:#c9c9c9'>$day[3]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:18px; color:#c9c9c9'>$day[4]</span></td></tr>";
echo "<tr><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:10px; color:#c9c9c9'>$icon[2]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:10px; color:#c9c9c9'>$icon[4]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:10px; color:#c9c9c9'>$icon[6]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:10px; color:#c9c9c9'>$icon[8]</span></td></tr>";
echo "<tr><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:10px; color:#c9c9c9'>$txt[2]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:10px; color:#c9c9c9'>$txt[4]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:10px; color:#c9c9c9'>$txt[6]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:10px; color:#c9c9c9'>$txt[8]</span></td></tr>"; 
echo "<tr><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:18px; color:#d30000'>$maxTemp[1]°</span>     |     <span style='font-size:18px; color:#006dd3'>$minTemp[1]°</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:18px; color:#d30000'>$maxTemp[2]°</span>     |     <span style='font-size:18px; color:#006dd3'>$minTemp[2]°</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:18px; color:#d30000'>$maxTemp[3]°</span>     |     <span style='font-size:18px; color:#006dd3'>$minTemp[3]°</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:18px; color:#d30000'>$maxTemp[4]°</span>     |     <span style='font-size:18px; color:#006dd3'>$minTemp[4]°</span></td></tr>";
echo "<tr><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'><i class='wi wi-raindrop' title='Opad szansa'></i> $deszcz[2]%</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'><i class='wi wi-raindrop' title='Opad szansa'></i> $deszcz[4]%</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'><i class='wi wi-raindrop' title='Opad szansa'></i> $deszcz[6]%</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'><i class='wi wi-raindrop' title='Opad szansa'></i> $deszcz[8]%</span></td></tr>";
echo "<tr><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'><i class='wi wi-raindrop' title='Opad szansa'></i> $windDir[2] $windSpeed[2] km/h</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'>$windDir[4] $windSpeed[4] km/h</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'>$windDir[6] $windSpeed[6] km/h</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'>$windDir[8] $windSpeed[8] km/h</span></td></tr>";
echo "<tr><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'><i class='wi wi-raindrop' title='Opad szansa'></i> $UV[2]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'>$UV[4]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'>$UV[6]</span></td><td style='padding: 8px;text-align: center;width: 25%'><span style='font-size:14px; color:#c9c9c9'>$UV[8]</span></td></tr>";
echo "</table>";

2

Answers


  1. file_get_contents will cache the result.

    This should solve it:

    $opts = [
      'http' => [
        'method' => 'GET',
        'header' => 'Cache-Control: no-cache',
      ],
    ];
    
    $context = stream_context_create($opts);
    
    $json = file_get_contents('https://api.weather.com/v3/wx/forecast/daily/5day?geocode=51.42,17.26&format=json&units=m&language=pl-PL&apiKey=MY API KEY', false, $context);
    

    But you could also fetch the data from the php using curl – wich is probably how I would do it.

    $url = 'https://api.weather.com/v3/wx/forecast/daily/5day?geocode=51.42,17.26&format=json&units=m&language=pl-PL&apiKey=MY API KEY';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cache-Control: no-cache'));
    
    $json = curl_exec($ch);
    
    if(curl_error($ch)) {
      echo 'Error: ' . curl_error($ch);
    }
    
    curl_close($ch);
    
    Login or Signup to reply.
  2. This is not likely a PHP problem, but something upstream. Either the data provider is giving you wonky data, or there is an improperly-configured or otherwise misbehaving intermediate proxy between you and wunderground that is causing it. You need to inspect the HTTP response headers, and file_get_contents() simply won’t accommodate this.

    Personally, I would switch to Guzzle and/or curl, but you can shimmy your way around it with fopen() and stream_get_meta_data():

    $url = 'https://api.weather.com/v3/wx/forecast/daily/5day?geocode=51.42,17.26&format=json&units=m&language=pl-PL&apiKey=MY API KEY';
    
    $fh = fopen($url, 'r');
    $json = stream_get_contents($fh);
    $headers = stream_get_meta_data($fh);
    

    The headers will tell you the date the information was generated, the cache lifetime, and give you a trace token that might be useful to tracking down the issue on the provider’s side. The headers may also contain evidence of a caching proxy causing problems in the middle.

    That said, I generally don’t recommend making API requests against 3rd parties for every single request to your site, as it’s likely that you will be making a lot of identical requests and either burning through your request allocations, or the goodwill of the provider. Implement caching in your app.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search