0

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]);
     }
rplantiko
  • 2,608
  • 20
  • 21
PHPNoob
  • 514
  • 6
  • 17
  • Where is `$btc` defined? Maybe you wanted `$btc = $data;` Also `var_dump($data);` – AbraCadaver Apr 22 '19 at 20:21
  • $btc is defined if its not empty. I just wrote the statement to see where the error was. I tried var_dump and its returning false. – PHPNoob Apr 22 '19 at 20:47
  • What does curl_getinfo() return after you've executed your request but before you close the connection? –  Apr 22 '19 at 20:54
  • Do you really have a newline between `Accept:` and `application/json`, or is that a copying error? – Barmar Apr 22 '19 at 20:58
  • @ReddHerring it returns nothing. – PHPNoob Apr 22 '19 at 20:58
  • @Barmar copy/paste error on SO. – PHPNoob Apr 22 '19 at 20:59
  • It really looks like you just have the assignment backwards, it should be `$btc = $data;`. – Barmar Apr 22 '19 at 21:02
  • Are you sure you have `curl` enabled? When I try your code on my machine it works (after flipping the assignment), but when I try it on ideone.com I get `nothing`. I guess they block `curl`. – Barmar Apr 22 '19 at 21:08
  • 1
    Change `print ("nothing");` to ` print ("nothing " . curl_error($curl));` so you see the reason why `curl` failed. On ideone.com it says "Could not resolve host: api.coindesk.com". – Barmar Apr 22 '19 at 21:11
  • @Barmar Seems like its a ssl error, as my error when I did that was "SSL certificate problem: unable to get local issuer certificate" – PHPNoob Apr 22 '19 at 21:15
  • Try using `http:` instead of `https:` – Barmar Apr 22 '19 at 21:16
  • 1
    See https://stackoverflow.com/questions/28858351/php-ssl-certificate-error-unable-to-get-local-issuer-certificate – Barmar Apr 22 '19 at 21:16
  • @Barmar removing the 's' corrected the issue. Thanks – PHPNoob Apr 22 '19 at 21:20
  • 1
    it would probably be better if you fixed your certificate, as there may be other sites that require SSL. – Barmar Apr 22 '19 at 21:28
  • @barmar yes I will, but at the moment this is hosted on a local server. When it goes live I will correct the ssl. – PHPNoob Apr 23 '19 at 13:32

0 Answers0