<?php
class Binance
{
private $base = 'https://api.binance.com/api/';
private $api = '';
private $api_method = '';
/********** 基础信息 **********/
/**
* @title 获取所有交易对
* @description 获取币安的所有交易对
* @author 开发者
* @url /api/binance/exchangeInfo
* @method GET
*/
public function exchangeInfo() {
$this->api_method = "v3/exchangeInfo";
$this->api = $this->base . $this->api_method;
$res = $this->request($this->api);
return json_decode($res,true);
}
/**
* @title 获取某币最新币价
* @description 获取币安的某币最新价格
* @author 开发者
* @url /api/binance/get_common_symbols
* @method GET
*
* @param $symbol string 例:BTCUSDT
*/
public function getPrice($symbol) {
$param = ["symbol"=>$symbol];
$this->api_method = "v3/ticker/price";
$this->api = $this->base . $this->api_method;
$res = $this->request($this->api,$param);
return json_decode($res,true);
}
/**
* 请求
*/
private function request($url, $params = [], $method = "GET") {
$opt = [
"http" => [
"method" => $method,
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\n"
]
];
$context = stream_context_create($opt);
$query = http_build_query($params, '', '&');
$res = file_get_contents($url.'?'.$query, false, $context);
return $res;
}
}
注:由于接口需在外网测试,建议采用外网服务器
本文介绍如何调用币安API来获取各种数字货币的实时价格信息,重点在于接口的使用方法和数据解析,适合对区块链和API接口有兴趣的开发者阅读。
5448

被折叠的 条评论
为什么被折叠?



