|
|
|
<?php
|
|
|
|
|
|
|
|
namespace trader\exchange\binance;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../struct/ApiInfo.php';
|
|
|
|
require_once __DIR__ . '/../../../jytools/func.php';
|
|
|
|
|
|
|
|
use trader\struct\ApiInfo;
|
|
|
|
use jytools\Curl;
|
|
|
|
use function jytools\getMicrotime;
|
|
|
|
|
|
|
|
class ApiSpot
|
|
|
|
{
|
|
|
|
private $host = 'https://api.binance.com';
|
|
|
|
private ?ApiInfo $apiInfo;
|
|
|
|
public function __construct(?ApiInfo $apiInfo)
|
|
|
|
{
|
|
|
|
$this->apiInfo = $apiInfo;
|
|
|
|
}
|
|
|
|
public function setHost($host)
|
|
|
|
{
|
|
|
|
$this->host = $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------public interface ------------
|
|
|
|
//获取所有品种资金费率
|
|
|
|
public function getPremiumIndex($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/premiumIndex";
|
|
|
|
$method = "GET";
|
|
|
|
return $this->request($method, $url, $params);
|
|
|
|
}
|
|
|
|
//获取交易对信息
|
|
|
|
public function getExchangeInfo($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/exchangeInfo";
|
|
|
|
$method = "GET";
|
|
|
|
return $this->request($method, $url, $params);
|
|
|
|
}
|
|
|
|
//获取k线
|
|
|
|
public function klines($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/klines";
|
|
|
|
$method = "GET";
|
|
|
|
return $this->request($method, $url, $params);
|
|
|
|
}
|
|
|
|
//获取深度
|
|
|
|
public function depth($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/depth";
|
|
|
|
$method = "GET";
|
|
|
|
return $this->request($method, $url, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------private interface ------------
|
|
|
|
//获取用户监听key
|
|
|
|
public function getListenKey($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/listenKey";
|
|
|
|
$method = "POST";
|
|
|
|
return $this->request($method, $url, [], $this->apiInfo);
|
|
|
|
}
|
|
|
|
//设置持仓模式
|
|
|
|
public function setPositionMode($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/positionSide/dual";
|
|
|
|
$method = "POST";
|
|
|
|
return $this->request($method, $url, $params, $this->apiInfo);
|
|
|
|
}
|
|
|
|
//获取账户持仓风险v2
|
|
|
|
public function getPositionRiskV2($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v2/positionRisk";
|
|
|
|
$method = "GET";
|
|
|
|
return $this->request($method, $url, $params, $this->apiInfo);
|
|
|
|
}
|
|
|
|
//下单
|
|
|
|
public function placeOrder($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/order";
|
|
|
|
$method = "POST";
|
|
|
|
return $this->request($method, $url, $params, $this->apiInfo);
|
|
|
|
}
|
|
|
|
//设置杠杆
|
|
|
|
public function setLever($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/leverage";
|
|
|
|
$method = "POST";
|
|
|
|
return $this->request($method, $url, $params, $this->apiInfo);
|
|
|
|
}
|
|
|
|
//查询仓位
|
|
|
|
public function accountV3($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v3/account";
|
|
|
|
$method = "GET";
|
|
|
|
return $this->request($method, $url, $params, $this->apiInfo);
|
|
|
|
}
|
|
|
|
//查询交易对配置
|
|
|
|
public function symbolConfig($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/symbolConfig";
|
|
|
|
$method = "GET";
|
|
|
|
return $this->request($method, $url, $params, $this->apiInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
//现货
|
|
|
|
public function account($param){
|
|
|
|
$url = "/api/v3/account";
|
|
|
|
$method = "GET";
|
|
|
|
return $this->request($method, $url, $param, $this->apiInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------
|
|
|
|
|
|
|
|
private function createSign($secret, $param)
|
|
|
|
{
|
|
|
|
$len = count($param);
|
|
|
|
if ($len == 0) {
|
|
|
|
return $param;
|
|
|
|
}
|
|
|
|
$paramStr = http_build_query($param);
|
|
|
|
return hash_hmac('sha256', $paramStr, $secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function request($method, $path, $param, ?ApiInfo $apiInfo = null)
|
|
|
|
{
|
|
|
|
$url = $this->host . $path;
|
|
|
|
if (!in_array(strtoupper($method), ['GET', 'POST', 'DELETE'])) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$header = [];
|
|
|
|
if ($apiInfo != null) {
|
|
|
|
$param['timestamp'] = getMicrotime();
|
|
|
|
$param['signature'] = $this->createSign($apiInfo->secret, $param);
|
|
|
|
$header[] = "X-MBX-APIKEY:" . $apiInfo->key;
|
|
|
|
}
|
|
|
|
$data = json_encode([]);
|
|
|
|
if (strtoupper($method) == 'POST') {
|
|
|
|
$data = Curl::httpPost($url, $param, $header);
|
|
|
|
}
|
|
|
|
if (strtoupper($method) == 'GET') {
|
|
|
|
$data = Curl::httpGet($url, $param, $header);
|
|
|
|
}
|
|
|
|
if (strtoupper($method) == 'DELETE') {
|
|
|
|
$data = Curl::httpDelete($url, $param, $header);
|
|
|
|
}
|
|
|
|
return json_decode($data, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
...
|
...
|
|