【问题标题】:PHP opens and closes file, but does not write anything to itPHP打开和关闭文件,但不向其中写入任何内容
【发布时间】:2021-01-21 09:56:30
【问题描述】:

我已经花了几个小时来阅读这篇文章,但到目前为止我还没有找到明确的解决方案....我正在使用 WAMP 作为我的本地服务器运行。我设置了一个成功的 API 调用来返回数据。 我想将这些数据存储在本地,从而减少 API 调用的次数。

为简单起见,我在与我的 PHP 脚本相同的文件夹中创建了一个 cache.json 文件,当我运行该过程时,我可以看到该文件已在时间戳更新时被访问。

但文件仍然是空的。

根据研究,我怀疑问题可能归结为权限问题;我已经浏览了文件夹和文件,并且未选中只读等。

感谢是否有人可以验证我的代码是否正确,以及是否有希望为我指明解决方案的方向。

非常感谢

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


?>

【问题讨论】:

  • 检查你的 $json_cache 是否不为空。这很生硬,但可能是这样。如果您使用的是 linux,请确保您也将其 chmod 为“写入”。阅读本文,它可能会有所帮助stackoverflow.com/questions/52381052/… 最后它确实取决于您从哪里运行代码,URL 或 cmd 行。
  • 也许您必须更改您尝试写入的文件夹的权限 - 可能是只读的。
  • 我不知道 cURL,但请尝试将 curl_close($ch); 移动到您写完的地方。也启用错误报告。
  • 感谢您的回复,我已经设法通过使用 file_put_contents() 解决了这个问题。不知道为什么这行得通,而另一种方式则行不通,反正暂时解决了 ;-) 我会更新新的代码块。

标签: php file curl


【解决方案1】:

我设法通过使用 file_put_contents() 解决了这个问题,我不是专家,我不明白为什么它会起作用,而上面的代码不起作用,但也许这对其他人有帮助。

调整后的代码:

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

?>

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多