我正试图从这个JSON中提取一些加密货币的价格:
https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH&tsyms=XRP,BCH,LTC,NEO,ADA,XLM,EOS,XMR,DASH
当你直接访问它或通过Postman访问它时,它可以正常工作,但当我试图使用它时会出现错误#4的响应。
代码:
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);
第二个问题是如何将响应格式化为:
foreach ($currencty_res[0] as $key => $item) {
if (!empty($item)) {
$currencyA = $item;
$currencyB = $item[0];
$value = $item[0][0];
echo $currencyA . '/' . $currencyB . ' : ' . $value;
}
}
因此,每个货币对都会产生回显BTC/XRP : 11515.43
。
BTC/XRP : 11515.43
BTC/BCH : 8.87
BTC/LTC : 49.83
...
ETH/XRP : 898.2
ETH/BCH : 0.6921
ETH/LTC : 3.89
答案 0 :(得分:2)
我做了一点改动,现在正在工作(见评论)
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);
结果是:
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
)
)
对于第二个问题,试试这个:
foreach ($currencty_res as $currencyA => $array_currencyB) {
foreach($array_currencyB as $currencyB => $value){
echo $currencyA . '/' . $currencyB . ' : ' . $value."<br>";
}
}
返回:
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