doutan1905 2019-04-23 02:19
浏览 546
已采纳

PHP CURL错误:数据返回NULL值

I have a JSON code which I put in JSON file named data.json

the JSON Code is :

[
 {"value":"23","date":"03/2018"},
 {"value":"43","date":"03/2019"},
 {"value":"34","date":"12/2017"},
 {"value":"13","date":"01/2019"},
 {"value":"34","date":"02/2019"}
]

Now The php Code I am using to fetch the data is as Follows :

$url = "data.json" ;

$ch = curl_init() ;

curl_setopt($ch, CURLOPT_URL, $url) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch);


curl_close($ch) ;


$data = json_decode($result, true) ;

foreach($data as $mydata)
{
  $data1 = $mydata["value"] ;
  $data2 = $mydata["date"] ;

echo $data1 ." : ". $data2 ;
echo "<br/>";

}

I Don't understand what's wrong with the code but it gives an error Warning

Invalid argument supplied for foreach()

This Happens Because the Data is returning NULL Value But I Don't How my JSON Data Format is not correct. THe Format Looks Fine. But it still returns NULL Value.

展开全部

  • 写回答

4条回答 默认 最新

  • dongliang1873 2019-04-23 02:58
    关注

    You have to provide the full URL of the JSON file if you are using CURL, instead of the data.json

    http://localhost/text.json 
    

    other options to get the data from json

    $result = file_get_contents($url);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥20 游戏mod是如何制作的
  • ¥15 关于#hadoop#的问题:按照老师上课讲的步骤写的
  • ¥20 有人会用这个工具箱吗 付fei咨询
  • ¥30 成都市武侯区住宅小区兴趣点
  • ¥15 Windows软实时
  • ¥15 自有服务器搭建网络隧道并且负载均衡
  • ¥15 opencv打开dataloader显示为nonetype
  • ¥15 MacOS 80端口外网无法访问
  • ¥50 js逆转反解密-会的来
  • ¥15 wrodpress如何调取数据库并展示
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部