将JSON输出保存到JSON文件中。

5

我得到了这段代码:

<?php
  $jsonurl = "https://newsapi.org/v1/articles?source=cnn&sortBy=top&apiKey=d8dfc55aa005430c9567416cac34e3fa";
  $json = file_get_contents($jsonurl);
  var_dump(json_decode($json));

          $file = fopen('news.json','w');
          fwrite($file, $json);
          fclose($file);

但是没有任何东西被保存到新的news.json文件中,为什么会这样呢?按照我的教导-创建文件使用这种方法。(在fopen上使用w


在我的端上运行良好。 - Alive to die - Anant
正常运行 PHP 7.0 - M A SIDDIQUI
2个回答

5
请检查您目录下的写入权限。同时指定news.json文件的绝对路径,因为似乎您的PHP服务器无法读取相对路径:
$file = fopen('news.json','w'); 

to

$file = fopen(__DIR__ . '/news.json','w');

2

启用PHP错误以查看问题,您是否在var_dump中看到数据(可能是SSL问题)?还请检查news.json文件的权限。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接