PHP opens and closes file, but does not write anything to it

Tags: , ,



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);


?>

Answer

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);  

?>


Source: stackoverflow