BinanceSpot.php
4.0 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
<?php
namespace Jiaoyin;
use Jiaoyin;
use Jiaoyin\Curl;
/*
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);
}
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 = network\Curl::httpPost($url, $param);
}
if (strtoupper($method) == 'GET') {
$data = network\Curl::httpGet($url, $param);
}
if (strtoupper($method) == 'DELETE') {
$data = network\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);
}
/*
* ==================================
* 以下公共接口,无限apikey
* ==================================
*/
//24小时成交数据
// static public function ticker24hr($param = [])
// {
// $path = '/fapi/v1/ticker/24hr';
// $method = 'GET';
// return self::requestMarket($method, $path, $param);
// }
}
?>