简体   繁体   中英

JSON response is null with error 4

I'm trying to extract the prices of some cryptocurrencies from this JSON:

https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH&tsyms=XRP,BCH,LTC,NEO,ADA,XLM,EOS,XMR,DASH

It works perfectly fine when you access it directly or thru Postman but a response of error #4 appears when I'm trying to cURL it.

Code:

    header('Content-Type: text/html; charset=utf-8', true);
function getCurrencies()
{
    try {
        $curlDefault = array(
            CURLOPT_PORT => 80, 
            CURLOPT_SSL_VERIFYPEER => TRUE,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_FOLLOWLOCATION => TRUE,
            CURLOPT_ENCODING => '',
            CURLOPT_HTTPHEADER => array(
                'Proxy-Connection: Close',
                'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1017.2 Safari/535.19',
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Encoding: gzip,deflate,sdch',
                'Accept-Language: en-US,en;q=0.8',
                'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3',
                'Cookie: __qca=blabla',
                'Connection: Close',
            ),
            CURLOPT_VERBOSE => TRUE
        );

        $url = "https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH&tsyms=XRP,BCH,LTC,NEO,ADA,XLM,EOS,XMR,DASH";

        $handle = curl_init($url);
        curl_setopt_array($handle, $curlDefault);
        $res = curl_exec($handle);
        curl_close($handle);
        return $res;
    } catch (Exception $e) {

        trigger_error(sprintf(
            'Curl failed with error #%d: %s',
            $e->getCode(), $e->getMessage()),
            E_USER_ERROR);

    }
}


$currencty_res = json_decode(getCurrencies(), true);

if (is_null($currencty_res)) {
   die("Json decoding failed with error: ". json_last_error());
}

var_dump($currencty_res);

The second question would be how to format the response to something like:

foreach ($currencty_res[0] as $key => $item) {

if (!empty($item)) {
    $currencyA = $item;
    $currencyB = $item[0];
    $value = $item[0][0];

    echo $currencyA . '/' . $currencyB . ' : ' . $value;
    }

}

So it will result in echo BTC/XRP : 11515.43 for each of the currency pairs.

BTC/XRP : 11515.43
BTC/BCH : 8.87
BTC/LTC : 49.83
...
ETH/XRP : 898.2
ETH/BCH : 0.6921
ETH/LTC : 3.89

I have made little changes and now work (see comments)

function getCurrencies() {

     $url = "https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH&tsyms=XRP,BCH,LTC,NEO,ADA,XLM,EOS,XMR,DASH";
    $options = array( 
        CURLOPT_RETURNTRANSFER => true,   // return web page
        CURLOPT_HEADER         => false,  // don't return headers         
        CURLOPT_FOLLOWLOCATION => true,   // follow redirects
        CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
        CURLOPT_ENCODING       => "",     // handle compressed
        CURLOPT_USERAGENT      => $_SERVER['HTTP_USER_AGENT'], // name of client
        CURLOPT_AUTOREFERER    => false,   // set referrer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect 
        CURLOPT_TIMEOUT        => 1,    // time-out on response
          CURLOPT_SSL_VERIFYPEER => false, //avoid ssl autentication
    ); 

    $ch = curl_init($url);
    curl_setopt_array($ch, $options);

    $content  = curl_exec($ch);

    curl_close($ch);

    return $content;
}


$currencty_res = json_decode(getCurrencies(), true);

try_again:
if(!$currencty_res){
    sleep(2);
    //I saw some time fail so try_again;
    goto try_again;
}

print_r($currencty_res);

The result is:

Array
(
    [BTC] => Array
        (
            [XRP] => 11481.06
            [BCH] => 8.86
            [LTC] => 49.68
            [NEO] => 103.34
            [ADA] => 43859.65
            [XLM] => 30892.8
            [EOS] => 1529.29
            [XMR] => 33.46
            [DASH] => 18.46
        )

    [ETH] => Array
        (
            [XRP] => 895.06
            [BCH] => 0.6905
            [LTC] => 3.87
            [NEO] => 8.06
            [ADA] => 3419.3
            [XLM] => 2408.4
            [EOS] => 119.32
            [XMR] => 2.61
            [DASH] => 1.44
        )

)

For the second question try this:

foreach ($currencty_res as $currencyA => $array_currencyB) {

    foreach($array_currencyB as $currencyB => $value){
        echo $currencyA . '/' . $currencyB . ' : ' . $value."<br>";
    }
}

That return:

BTC/XRP : 11436.41
BTC/BCH : 8.83
BTC/LTC : 49.65
BTC/NEO : 103.1
BTC/ADA : 43802.01
BTC/XLM : 30864.2
BTC/EOS : 1525.09
BTC/XMR : 33.5
BTC/DASH : 18.47
ETH/XRP : 893.3
ETH/BCH : 0.69
ETH/LTC : 3.88
ETH/NEO : 8.05
ETH/ADA : 3421.38
ETH/XLM : 2410.8
ETH/EOS : 119.23
ETH/XMR : 2.62
ETH/DASH : 1.44

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM