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 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测