-1

I am trying to retrieve the JSON from the URL by using curl but it is returning empty data from this url

$curl =curl_init('https://www.bloomberg.com/markets/api/security/currency/cross-rates/PKR,USD,AED,GBP,SAR,AUD,CAD,EUR,BHD,CHF,CNY,DKK,HKD,INR,JPY,KWD,MYR,NOK,NZD,OMR,QAR,SEK,THB');

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($curl);
print_r($page)

Many thanks in advance.

  • 3
    Possible duplicate of [How to use cURL to get jSON data and decode the data?](https://stackoverflow.com/questions/16700960/how-to-use-curl-to-get-json-data-and-decode-the-data) – KhorneHoly Oct 09 '18 at 10:45
  • try `$page = file_get_contents("https://example.com/")` – Chris Oct 09 '18 at 10:51

3 Answers3

0

try using file_get_contents:

$jsonString = file_get_contents('https://www.bloomberg.com/markets/api/security/currency/cross-rates/PKR,USD,AED,GBP,SAR,AUD,CAD,EUR,BHD,CHF,CNY,DKK,HKD,INR,JPY,KWD,MYR,NOK,NZD,OMR,QAR,SEK,THB');

$jsonArray = json_decode($jsonString, true);
Doğan Uçar
  • 166
  • 3
  • 15
0

your php/libcurl is blocked from accessing the interwebs in some way. enable CURLOPT_VERBOSE, record the VERBOSE log, and take it up with your local sysadmin, it's probably his doing (perhaps a firewall or iptables rules or something disallowing php internet access? perhaps an incorrectly configured dns server? your sysadmin should know.) add this to get a VERBOSE log:

curl_setopt($curl,CURLOPT_VERBOSE,1);
curl_setopt($curl,CURLOPT_STDERR,fopen("php://output","wb"));
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
0

Try this code:

    $ch = curl_init();
    $data_string = json_encode($data); // OR $data_string = http_build_query($data);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);