doucaigai7176 2019-04-22 20:11
浏览 38

cURL变量为空

I am using charts.js and trying to load data from a JSON API using cURL to pass to the chart. So I am using a PHP variable to pass to JavaScript. I did a test in ajax and it worked, but wanting to use cURL I cannot figure out the issue.

I created an if statement that it will print out nothing on an empty variable and that's what it has been doing, so I believe the issue is with cURL.

<?php 

$url = "https://api.coindesk.com/v1/bpi/historical/close.json?currency=btc";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);

if(!empty($data)) {
   $data = $btc;
} else {
    print ("nothing");
}
curl_close($curl);


?>

<body>
      <canvas id="myChart" width="250px" height="250px"></canvas>
 <script>
     jsonData=<?php echo $btc ?>;
     var jsonLabels=[];
     var jsonValues=[];
     for(x in jsonData['bpi']){
         jsonLabels.push(x);
         jsonValues.push(jsonData['bpi'][x]);
     }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

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