doutan1905 2019-04-23 10: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 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 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器