Binance API - 仅获取 XXX/GBP 的 Klines

Binance API - Get Klines for XXX/GBP only

我打电话给 Binance Klines API 以获取当前价格。


// Get Assets - ideally I'd just like the currency name (e.g. ETH) rather ETC/BTC

$url = 'https://api.binance.com/api/v3/exchangeInfo';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);

$json = json_decode($result);

foreach($json->symbols as $symbol)
{
   // Get prices for teh asset

   $ccode = $symbol->symbol;
   $nurl = 'https://api.binance.com/api/v3/klines?symbol=' . $ccode . '&interval=1m';

   $stime = 1000*(time() - 60); // Time period is the last minute
   $etime = 1000*time();

   $nurl .= '&startTime=' . $stime; 
   $nurl .= '&endTime=' . $etime;
   
   $ch = curl_init($nurl);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_URL, $nurl);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   $json = curl_exec($ch);
   curl_close($ch);

   if ( $json === "" || $json === "[]" || substr($json, 0, 8) == '{"code":' )
   {
      echo "Not found " . $ccode . " .. skipping <BR>";
      continue;
   }
      
   $arr = explode(",", $json);

   $price = $arr[4];
   $tstamp1 = $arr[6];
   $tstamp = gmdate("Y-m-d\TH:i:s\Z", ($arr[6]/1000));
   echo  $ccode . " " .  $tstamp . " " . $price . "<BR>";
}

问题是当我只想要每个硬币的英镑价格时,我得到了 coin/currency 的所有组合。关于超时的完整列表运行超过 5 分钟。

我只想获得每枚代币的英镑价格。

我该怎么做?

您正在请求所有符号 (exchangeInfo),然后获取每个符号(= 货币对)的蜡烛图信息 (klines)。

您可以仅针对英镑对执行此操作,方法是在您当前迭代的两种货币中查找英镑,方法是将其添加到您的 foreach 中:

// rest of the code

foreach($json->symbols as $symbol)
{
   if ($symbol->baseAsset === "GBP" || $symbol->quoteAsset === "GBP" ) {

    // rest of your foreach
       
   }
   
}

在您的 foreach 中,您在 $symbol 下拥有这三个属性,您可以利用:

$symbol->symbol    // "ADAGBP"
$symbol->baseAsset // "ADA"
$symbol->quoteAsset // "GBP"