Api.php
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace trader\exchange\binance;
require_once __DIR__ . '/../../struct/ApiInfo.php';
require_once __DIR__ . '/../../../jiaoyin/func.php';
use trader\struct\ApiInfo;
use Jiaoyin\Curl;
use function Jiaoyin\getMicrotime;
class Api
{
private $host = 'https://fapi.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 cancelOrder($params)
{
$url = "/fapi/v1/order";
$method = "DELETE";
return $this->request($method, $url, $params, $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);
}
}