1

I have spent a couple of hours reading up on this but as so yet I find no clear solutions....I am using WAMP to run as my Local server. I have a successful API call set up to return data. I would like to store that data locally, thus reducing the number of API call being made.

For simplicity I have created a cache.json file in the same folder as my PHP scripts and when I run the process I can see the file has been accessed as the time stamp updates.

But the file remains empty.

Based on research I suspect the issue may come down to a permission issue; I have gone through the folders and files and unchecked read only etc.

Appreciate if someone could validate that my code is correct and if it is hopefully point me in the the direction of a solution.

many thanks

<?php

    $url = 'https://restcountries.eu/rest/v2/name/'. $_REQUEST['country'];

    $cache          = __DIR__."/cache.json"; // make this file in same dir
    $force_refresh  = true; // dev
    $refresh        = 60; // once an min (set short time frame for testing)

    // cache json results so to not over-query (api restrictions)
    if ($force_refresh || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) {


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);

    $result=curl_exec($ch);

    curl_close($ch);

    $decode = json_decode($result,true);


    $handle = fopen($cache, 'w');// or die('no fopen'); 
    $json_cache = $decode;
    fwrite($handle, $json_cache);
    fclose($handle);

     }
} else {
    $json_cache = file_get_contents($cache); //locally
}

    echo json_encode($json_cache, JSON_UNESCAPED_UNICODE);


?>

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
trig79
  • 384
  • 3
  • 12
  • Check if your $json_cache is not empty. Thats blunt but might be the case. If you are using linux, make sure you chmoded it to "write" aswell. Read this, it might help https://stackoverflow.com/questions/52381052/write-json-data-to-text-file-with-php finally it does depends where are you running the code from, URL or cmd line. – Thomas J. Oct 06 '20 at 13:53
  • Maybe You have to change permissions to folder where You try to write - maybe its only read. – Jakub Ujvvary Oct 06 '20 at 13:57
  • I don't know cURL but try moving `curl_close($ch);` where you're done writing. Enable error reporting also. – Funk Forty Niner Oct 06 '20 at 14:03
  • thanks for replying, I have managed to solve this by using file_put_contents(). NO IDEA why this works and the other way doesn't, anyway for time being its resolved ;-) I'll update the new code block. – trig79 Oct 06 '20 at 14:46

1 Answers1

1

I managed to solve this by using file_put_contents(), not being an expert I do not understand why this works and the code above doesn't, but maybe this helps someone else.

adjusted code:

<?php

    $url = 'https://restcountries.eu/rest/v2/name/'. $_REQUEST['country'];

    $cache          = __DIR__."/cache.json"; // make this file in same dir
    $force_refresh  = false; // dev
    $refresh        = 60; // once an min (short time frame for testing)

// cache json results so to not over-query (api restrictions)
    if ($force_refresh || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) {


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);

    $result=curl_exec($ch);

    curl_close($ch);

    $decode = json_decode($result,true);

    $handle = fopen($cache, 'w');// or die('no fopen'); 
    $json_cache = $result;

    file_put_contents($cache, $json_cache);

} else {
    $json_cache = file_get_contents($cache); //locally
    $decode = json_decode($json_cache,true);

}

    echo json_encode($decode, JSON_UNESCAPED_UNICODE);  

?>
trig79
  • 384
  • 3
  • 12