doutan1905 2019-04-23 10:19
浏览 545
已采纳

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 10: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条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题