BinanceUnified.php
5.8 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
<?php
namespace Jiaoyin;
/*
binance现货接口
*/
class BinanceUnified
{
static private string $host = 'https://papi.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);
}
/**
* @param string $path (binance api path)
* @param string $method (GET POST DELETE)
* @param array $param (binance api param)
* @param int $type (=1:行情等接口,不需要签名;=2:账户等接口需要签名)
* @return array|int|mixed
*/
public function request(string $path, string $method, array $param, int $type=1)
{
if($type==2){
return self::requestAccount($method, $path, $param);
}else if($type==1){
return self::requestMarket($method, $path, $param);
}else{
return ['code'=>1000,'msg'=>'type参数错误'];
}
}
//获取账户余额
public function getUserAsset($param=[])
{
$path = '/papi/v1/balance';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//获取Listen Key (USER_STREAM)
public function listenKey($param=[])
{
$path = '/papi/v1/listenKey';
$method = 'POST';
return $this->requestListenKey($method, $path, $param);
}
//延长 Listen Key 有效期 (USER_STREAM)
public function listenKeyDelay($param=[])
{
$path = '/papi/v1/listenKey';
$method = 'PUT';
return $this->requestListenKey($method, $path, $param);
}
//现有UM账户资产和仓位信息【期货合约(永续合约、交割合约)】
public function accountUm($param=[])
{
$path = '/papi/v1/um/account';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
//获取现有CM账户资产和仓位信息【现货市场(包括杠杆借贷现货交易)】
public function accountCm($param=[])
{
$path = '/papi/v1/cm/account';
$method = 'GET';
return $this->requestAccount($method, $path, $param);
}
// public function order($param)
// {
// $path = '/api/v3/order';
// $method = 'POST';
// return $this->requestAccount($method, $path, $param);
// }
//
// public function cancelOrderById($param){
// $path = '/api/v3/order';
// $method = 'DELETE';
// return $this->requestAccount($method, $path, $param);
// }
//
// public function openOrders($param){
// $path = '/api/v3/openOrders';
// $method = 'GET';
// return $this->requestAccount($method, $path, $param);
// }
//
//统一账户账户生成 Listen Key (USER_STREAM)
}
?>