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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵