BinanceSpot.php
6.4 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
<?php
namespace Jiaoyin;
/*
binance现货接口
*/
class BinanceSpot
{
static private string $host = 'https://api.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);
}
private function requestAccount($method, $path, $param)
{
$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);
}
private function requestListenKey($method, $path, $param)
{
$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 private 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 getTransfer($param)
{
$path = '/sapi/v1/asset/transfer';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//查询子账户划转历史 (仅适用子账户)
public function subUserHistory($param)
{
$path = '/sapi/v1/sub-account/transfer/subUserHistory';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//合约资金划转
public function futuresTransfer($param)
{
$path = '/sapi/v1/futures/transfer';
$method = 'POST';
return $this->requestAccount($method, $path, $param);
}
//子账户向子账户划转
public function subToSub($param)
{
$path = '/sapi/v1/sub-account/transfer/subToSub';
$method = 'POST';
return $this->requestAccount($method, $path, $param);
}
//获取用户资产
public function getUserAsset($param)
{
$path = '/sapi/v3/asset/getUserAsset';
$method = 'POST';
return $this->requestAccount($method, $path, $param);
}
//查询子账户API Key IP白名单 (适用母账户)
public function ipRestrictionQuery($param)
{
$path = '/sapi/v1/sub-account/subAccountApi/ipRestriction';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//删除子账户API Key IP白名单 (适用母账户)
public function ipRestrictionDelete($param)
{
$path = '/sapi/v1/sub-account/subAccountApi/ipRestriction/ipList';
$method = 'DELETE';
return $this->requestAccount($method, $path, $param);
}
//为子账户API Key增加IP白名单 (适用母账户)
public function ipRestrictionAdd($param)
{
$path = '/sapi/v2/sub-account/subAccountApi/ipRestriction';
$method = 'POST';
return $this->requestAccount($method, $path, $param);
}
//查询子账户Margin/Futures状态 (适用主账户)
public function subAccountStatus($param)
{
$path = '/sapi/v1/sub-account/status';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//生成 Listen Key (USER_STREAM)
public function listenKey($param)
{
$path = '/api/v3/userDataStream';
$method = 'POST';
return $this->requestListenKey($method, $path, $param);
}
//延长 Listen Key 有效期 (USER_STREAM)
public function listenKeyDelay($param)
{
$path = '/api/v3/userDataStream';
$method = 'PUT';
return $this->requestListenKey($method, $path, $param);
}
/*
* ==================================
* 以下公共接口,无限apikey
* ==================================
*/
//24小时成交数据
// static public function ticker24hr($param = [])
// {
// $path = '/fapi/v1/ticker/24hr';
// $method = 'GET';
// return self::requestMarket($method, $path, $param);
// }
}
?>