BinanceFutures.php
6.9 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
namespace jiaoyin;
/*
binance合约接口
*/
class BinanceFutures
{
static private string $host = 'https://fapi.binance.com';
private string $apikey = '';
private string $secret = '';
public function __construct($apikey = '', $secret = '', $host = '')
{
$this->apikey = $apikey;
$this->secret = $secret;
if (!empty($host)) {
self::$host = $host;
}
}
static private function createUrl($path): string
{
return self::$host . $path;
}
static private function createHeader($apikey): array
{
return ["X-MBX-APIKEY:" . $apikey];
}
static private function createSign($secret, $param)
{
$len = count($param);
if ($len == 0) {
output('param 为空,签名失败');
return $param;
}
$paramStr = http_build_query($param);
return hash_hmac('sha256', $paramStr, $secret);
}
public function requestAccount($method, $path, $param)
{
if (empty($this->apikey) || empty($this->secret)) {
output('api 或 secret 为空');
return ['code' => -1, 'msg' => 'apikey or secret is empty'];
}
$url = $this->createUrl($path);
$param['timestamp'] = getMicrotime();
$param['signature'] = $this->createSign($this->secret, $param);
$header = $this->createHeader($this->apikey);
if (!in_array(strtoupper($method), ['GET', 'POST', 'DELETE'])) {
output('请求方法错误', $method, $path, $param);
return 0;
}
$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);
}
static public function requestMarket($method, $path, $param)
{
$url = self::createUrl($path);
if (!in_array(strtoupper($method), ['GET', 'POST', 'DELETE'])) {
output('请求方法错误', $method, $path, $param);
return 0;
}
$data = json_encode([]);
if (strtoupper($method) == 'POST') {
$data = Curl::httpPost($url, $param);
}
if (strtoupper($method) == 'GET') {
$data = Curl::httpGet($url, $param);
}
if (strtoupper($method) == 'DELETE') {
$data = Curl::httpDelete($url, $param);
}
return json_decode($data, true);
}
public function cancelOrderById($param)
{
$path = '/fapi/v1/order';
$method = 'DELETE';
return $this->requestAccount($method, $path, $param);
}
//查询所有挂单
public function openOrders($param)
{
$path = '/fapi/v1/openOrders';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//取消所有订单
public function cancelAllOpenOrders($param)
{
$path = '/fapi/v1/allOpenOrders';
$method = 'DELETE';
return $this->requestAccount($method, $path, $param);
}
//下单
public function order($param)
{
$path = '/fapi/v1/order';
$method = 'POST';
return $this->requestAccount($method, $path, $param);
}
//获取或者延长listenKey
public function listenKey($param = [])
{
$path = '/fapi/v1/listenKey';
$method = 'POST';
return $this->requestAccount($method, $path, $param);
}
//获取仓位
public function account($param = [])
{
$path = '/fapi/v2/account';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//查询历史订单
public function allOrders($param)
{
$path = '/fapi/v1/allOrders';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//查询历史成交子订单
public function userTrades($param)
{
$path = '/fapi/v1/userTrades';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//查询持仓模式
public function positionSideDual($param = [])
{
$path = '/fapi/v1/positionSide/dual';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//设置持仓模式
public function setPositionSideDual($param)
{
$path = '/fapi/v1/positionSide/dual';
$method = 'POST';
return $this->requestAccount($method, $path, $param);
}
//查询资产
public function balance($param = [])
{
$path = '/fapi/v2/balance';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//查询杠杆层级
public function leverageBracket($param = [])
{
$path = '/fapi/v1/leverageBracket';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//查询用户持仓风险,杠杆倍数
public function positionRisk($param = [])
{
$path = '/fapi/v2/positionRisk';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//设置杠杆
public function leverage($param)
{
$path = '/fapi/v1/leverage';
$method = 'POST';
return $this->requestAccount($method, $path, $param);
}
//查询资金流水
public function income($param)
{
$path = '/fapi/v1/income';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//rateLimit
public function rateLimit($param)
{
$path = '/fapi/v1/rateLimit/order';
$method = 'GET';
return self::requestAccount($method, $path, $param);
}
/*
* ==================================
* 以下公共接口,无限apikey
* ==================================
*/
//24小时成交数据
static public function ticker24hr($param = [])
{
$path = '/fapi/v1/ticker/24hr';
$method = 'GET';
return self::requestMarket($method, $path, $param);
}
//获取交易对和交易规则
static public function exchangeInfo($param = [])
{
$path = '/fapi/v1/exchangeInfo';
$method = 'GET';
return self::requestMarket($method, $path, $param);
}
//获取k线数据
static public function klines($param)
{
$path = '/fapi/v1/klines';
$method = 'GET';
return self::requestMarket($method, $path, $param);
}
//获取历史持仓
static public function openInterestHist($param)
{
$path = '/futures/data/openInterestHist';
$method = 'GET';
return self::requestMarket($method, $path, $param);
}
}