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]);
}