OkApi.php
4.5 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
<?php
namespace jiaoyin;
/*
所有接口
*/
class OkApi
{
protected string $host = 'https://www.okx.com';
protected string $apikey = '';
protected string $secret = '';
protected string $apipwd = '';
protected string $nonce = '';
protected string $signature = '';
protected array $headers = [];
protected string $method = '';
protected array $param = [];
protected string $path = '';
protected array $options = [];
public function __construct($apikey = '', $secret = '', $apipwd = '', $host = '')
{
$this->apikey = $apikey;
$this->secret = $secret;
$this->apipwd = $apipwd;
if (!empty($host)) {
$this->host = $host;
}
}
// 核心认证过程
protected function auth()
{
$this->generateNonce();
$this->generateSignature();
$this->createHeaders();
}
// 生成时间戳
protected function generateNonce()
{
$this->nonce = gmdate('Y-m-d\TH:i:s\.000\Z');
}
// 生成签名
protected function generateSignature()
{
$body = '';
$path = $this->method . $this->path;
if (!empty($this->param)) {
if ($this->method == 'GET') {
$path .= '?' . http_build_query($this->param);
} else {
$body = json_encode($this->param);
}
}
$this->signature = base64_encode(hash_hmac('sha256', $this->nonce . $path . $body, $this->secret, true));
}
// 构建请求URL
private function createUrl($path): string
{
return $this->host . $path;
}
// 构建请求头
private function createHeaders(): array
{
$headers = [
'Content-Type' => 'application/json',
];
if (!empty($this->apikey) && !empty($this->secret)) {
$headers = array_merge($headers, [
'OK-ACCESS-KEY' => $this->apikey,
'OK-ACCESS-TIMESTAMP' => $this->nonce,
'OK-ACCESS-PASSPHRASE' => $this->apipwd,
'OK-ACCESS-SIGN' => $this->signature,
]);
}
$headers_array = [];
foreach ($headers as $key => $value) {
$headers_array[] = $key . ':' . $value;
}
$this->headers = $headers_array;
return $headers;
}
/*
* 公共请求接口
* $type 1: 需鉴权 2: 不需鉴权
* */
public function request($method, $path, $param, $type = 1)
{
$this->method = strtoupper($method);
$this->path = $path;
$this->param = $param;
if ($type == 2) {
$this->createHeaders();
} else {
if (empty($this->apikey) || empty($this->secret) || empty($this->apipwd)) {
return ['code' => -1, 'msg' => 'apikey, secret or apipwd is empty'];
}
$this->auth();
}
if (!in_array($this->method, ['GET', 'POST', 'DELETE'])) {
return ['code' => -1, 'msg' => 'Invalid HTTP method'];
}
$url = $this->createUrl($path);
switch ($this->method) {
case 'GET':
$data = Curl::httpGet($url, $this->param, $this->headers);
break;
case 'POST':
$data = Curl::httpPost($url, $this->param, $this->headers);
break;
case 'DELETE':
$data = Curl::httpDelete($url, $this->param, $this->headers);
break;
}
return json_decode($data, true);
}
/**
* 合约账户余额查询
*/
public function getBalance($param = [])
{
$path = '/api/v5/account/balance';
return $this->request('GET', $path, $param);
}
/**
* 资金账户余额查询
*/
public function getAssetBalance($param = [])
{
$path = '/api/v5/asset/balances';
return $this->request('GET', $path, $param);
}
/**
* 获取持仓信息
*/
public function getPositions($param)
{
$path = '/api/v5/account/positions';
return $this->request('GET', $path, $param);
}
/*
* 资金划转
* */
public function transfer($param)
{
$path = '/api/v5/asset/transfer';
return $this->request('POST', $path, $param);
}
/*
* 公共请求接口
* */
public function getTransferHistory($path, $param)
{
$path = '/api/v5/asset/bills';
return $this->request('GET', $path, $param);
}
}