0

I am using weatherundeground.com to get weather data but I always reach the API calls limit so I was thinking about caching the json response every 60 minutes.

This is my simple php script

<?php
$json_string = file_get_contents("http://api.wunderground.com/api/apikey/conditions/forecast/lang:IT/q/CITY1.json");
$parsed_json = json_decode($json_string);

$city1 = $parsed_json->{'current_observation'}->{'display_location'}->{'city'};

I searched and found this answer: Caching JSON output in PHP

I tried to merge them like this:

$url = "http://api.wunderground.com/api/apikey/conditions/forecast/lang:IT/q/SW/Acquarossa.json";
function getJson($url) {
// cache files are created like cache/abcdef123456...
$cacheFile = 'cache' . DIRECTORY_SEPARATOR . md5($url) . '.json';

if (file_exists($cacheFile)) {
    $fh = fopen($cacheFile, 'r');
    $cacheTime = trim(fgets($fh));

    // if data was cached recently, return cached data
    if ($cacheTime > strtotime('-60 minutes')) {
        return fread($fh);
    }

    // else delete cache file
    fclose($fh);
    unlink($cacheFile);
}

$json = file_get_contents($url);

$fh = fopen($cacheFile, 'w');
fwrite($fh, time() . "\n");
fwrite($fh, $json);
fclose($fh);

return $json;
}

$json_string = getJson($url);

$parsed_json = json_decode($json_string);

$city1 = $parsed_json->{'current_observation'}->{'display_location'}->{'city'};

I have been able to set it up and now it gets the first "round" of data, but the 2nd one and all the following, give me an error:

Warning: fread() expects exactly 2 parameters, 1 given in /home/*****/public_html/*****/acquarossa.php on line 27. 

And if I put the cached json on any json validator, it says that it isn't a valid json.

( this is the cached file: http://spinnaker.url.ph/meteo/cache/1f58bbab7bf88f3f8561b769475cb7c1.json )

What can I do?

P.S.:I already CHMOD 777 the directory

3
  • Did you find the cache file on your harddrive (i.e. does it exist?). Any errors? Do you understand the code? If not, which part? Commented Aug 23, 2014 at 16:48
  • @Hirnhamster The file doesn't even get created. I think I should try to ask directly to the creator of the script of the answer( stackoverflow.com/questions/11407514/caching-json-output-in-php /user:deceze) Commented Aug 23, 2014 at 17:59
  • I have been able to set it up and now it gets the first "round" of data, but the 2nd one and all the following, give me an error: Warning: fread() expects exactly 2 parameters, 1 given in /home/u221293448/public_html/meteo/acquarossa.php on line 27. And if I put the cached json on any json validator, it says that it isn't a valid json. What can I do? ( this is the cached file: spinnaker.url.ph/meteo/cache/… ) Commented Aug 24, 2014 at 7:59

1 Answer 1

2

Actually, the warning is pretty clear about whats going wrong. fread() expects 2 parameters but you're only passing on one in line 27: return fread($fh);

Reading the manual for fread I guess you can fix this by changing

return fread($fh);

to

return fread($fh, filesize($chacheFile));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.