-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHP-sdk.php
287 lines (280 loc) · 9.97 KB
/
PHP-sdk.php
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
class Bitrue {
public $btc_value = 0.00;
protected $base = "https://www.bitrue.com/api/", $api_key, $api_secret;
public function __construct($api_key, $api_secret) {
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->header= '';
}
public function ping() {
return $this->request("v1/ping");
}
public function time() {
return $this->request("v1/time");
}
public function exchangeInfo() {
return $this->request("v1/exchangeInfo");
}
public function buy_test($symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
return $this->order_test("BUY", $symbol, $quantity, $price, $type, $flags);
}
public function sell_test($symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
return $this->order_test("SELL", $symbol, $quantity, $price, $type, $flags);
}
public function buy($symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
return $this->order("BUY", $symbol, $quantity, $price, $type, $flags);
}
public function sell($symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
return $this->order("SELL", $symbol, $quantity, $price, $type, $flags);
}
public function cancel($symbol, $orderid) {
return $this->signedRequest("v1/order", ["symbol"=>$symbol, "orderId"=>$orderid], "DELETE");
}
public function orderStatus($symbol, $orderid) {
if(!$orderid){
return $this->signedRequest("v1/order",["symbol"=>$symbol]);
}
return $this->signedRequest("v1/order",["symbol"=>$symbol, "orderId"=>$orderid]);
}
public function openOrders($symbol) {
return $this->signedRequest("v1/openOrders", ["symbol"=>$symbol]);
}
public function currentOrder($symbol, $limit = 500) {
return $this->signedRequest("v1/order",["symbol"=>$symbol, "limit"=>$limit]);
}
public function orders($symbol, $limit = 500) {
return $this->signedRequest("v1/allOrders",["symbol"=>$symbol]);
}
public function trades($symbol, $limit = 500) {
return $this->request("v1/trades",["symbol"=>$symbol, "limit"=>$limit]);
}
public function historyTrades($symbol, $limit = 500, $from_id=0) {
if($from_id) {
return $this->request("v1/historicalTrades",["symbol"=>$symbol, "limit"=>$limit, "formId"=>$from_id]);
}
return $this->request("v1/historicalTrades",["symbol"=>$symbol, "limit"=>$limit]);
}
public function aggTrades($symbol) {
return $this->request("v1/aggTrades", ["symbol"=>$symbol]);
}
//1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
public function candlesticks($symbol, $interval = "5m") {
return $this->request("v1/klines",["symbol"=>$symbol, "interval"=>$interval]);
}
public function prevDay($symbol='') {
return $this->request("v1/ticker/24hr", ["symbol"=>$symbol]);
}
public function myTrades($symbol) {
return $this->signedRequest("v1/myTrades", ["symbol"=>$symbol]);
}
public function price($symbol) {
return $this->request("v1/ticker/price", ["symbol"=>$symbol]);
}
public function prices() {
return $this->priceData($this->request("v1/ticker/allPrices"));
}
public function bookTicker($symbol) {
return $this->request("v1/ticker/bookTicker", ["symbol"=>$symbol]);
}
public function bookPrices() {
return $this->bookPriceData($this->request("v1/ticker/allBookTickers"));
}
public function account() {
return $this->signedRequest("v1/account");
}
public function depth($symbol) {
return $this->request("v1/depth",["symbol"=>$symbol]);
}
public function balances($priceData = false) {
return $this->balanceData($this->signedRequest("v1/account"),$priceData);
}
private function request($url, $params = [], $method = "GET") {
$opt = [
"http" => [
"method" => $method,
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Bitrue API)\r\n"
]
];
$headers = array('User-Agent: Mozilla/4.0 (compatible; PHP Bitrue API)',
'X-MBX-APIKEY: ' . $this->api_key,
'Content-type: application/x-www-form-urlencoded');
$context = stream_context_create($opt);
$query = http_build_query($params, '', '&');
$ret = $this->http_get($this->base.$url.'?'.$query, $headers);
return $ret;
}
private function signedRequest($url, $params = [], $method = "GET") {
if ( empty($this->api_key) ) die("signedRequest error: API Key not set!");
if ( empty($this->api_secret) ) die("signedRequest error: API Secret not set!");
$timestamp_t = $this->getServerTime();
if($timestamp_t < 0) {
$timestamp_t = number_format(microtime(true)*1000,0,'.','');
}
$params['timestamp'] = $timestamp_t;
$query = http_build_query($params, '', '&');
$signature = hash_hmac('sha256', $query, $this->api_secret);
$headers = array("User-Agent: Mozilla/4.0 (compatible; PHP Bitrue API)",
"X-MBX-APIKEY: {$this->api_key}",
"Content-type: application/x-www-form-urlencoded");
$opt = [
"http" => [
"method" => $method,
"ignore_errors" => true,
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Bitrue API)\r\nX-MBX-APIKEY: {$this->api_key}\r\nContent-type: application/x-www-form-urlencoded\r\n"
]
];
if ( $method == 'GET' ) {
// parameters encoded as query string in URL
$endpoint = "{$this->base}{$url}?{$query}&signature={$signature}";
$ret = $this->http_get($endpoint, $headers);
} else if ($method == 'POST'){
$endpoint = "{$this->base}{$url}";
$params['signature'] = $signature;
$ret = $this->http_post($endpoint, $params, $headers);
} else {
$endpoint = "{$this->base}{$url}?{$query}&signature={$signature}";
$ret = $this->http_other($method, $endpoint, $headers);
}
return $ret;
}
private function order_test($side, $symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
$opt = [
"symbol" => $symbol,
"side" => $side,
"type" => $type,
"quantity" => $quantity,
"recvWindow" => 60000
];
if ( $type == "LIMIT" ) {
$opt["price"] = $price;
$opt["timeInForce"] = "GTC";
}
// allow additional options passed through $flags
if ( isset($flags['recvWindow']) ) $opt['recvWindow'] = $flags['recvWindow'];
if ( isset($flags['timeInForce']) ) $opt['timeInForce'] = $flags['timeInForce'];
if ( isset($flags['stopPrice']) ) $opt['stopPrice'] = $flags['stopPrice'];
if ( isset($flags['icebergQty']) ) $opt['icebergQty'] = $flags['icebergQty'];
return $this->signedRequest("v1/order/test", $opt, "POST");
}
public function order($side, $symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
$side = strtoupper($side);
$type = strtoupper($type);
if (!in_array($side, ['BUY', 'SELL']) ) die("Unsupport side parameters, please check!");
if (!in_array($type, ['LIMIT', 'MARKET']) ) die("Unsupport type parameters, please check!");
$opt = [
"symbol" => $symbol,
"side" => $side,
"type" => $type,
"quantity" => $quantity,
"recvWindow" => 60000
];
if ( $type == "LIMIT" ) {
$opt["price"] = $price;
$opt["timeInForce"] = "GTC";
}
// allow additional options passed through $flags
if ( isset($flags['recvWindow']) ) $opt["recvWindow"] = $flags['recvWindow'];
if ( isset($flags['timeInForce']) ) $opt["timeInForce"] = $flags['timeInForce'];
if ( isset($flags['stopPrice']) ) $opt['stopPrice'] = $flags['stopPrice'];
if ( isset($flags['icebergQty']) ) $opt['icebergQty'] = $flags['icebergQty'];
return $this->signedRequest("v1/order", $opt, "POST");
}
private function balanceData($array, $priceData = false) {
if ( $priceData ) $btc_value = 0.00;
$balances = [];
foreach ( $array['balances'] as $obj ) {
$asset = $obj['asset'];
$balances[$asset] = ["available"=>$obj['free'], "onOrder"=>$obj['locked'], "btcValue"=>0.00000000];
if ( $priceData ) {
if ( $obj['free'] < 0.00000001 ) continue;
if ( $asset == 'BTC' ) {
$balances[$asset]['btcValue'] = $obj['free'];
$btc_value+= $obj['free'];
continue;
}
$btcValue = number_format($obj['free'] * $priceData[$asset.'BTC'],8,'.','');
$balances[$asset]['btcValue'] = $btcValue;
$btc_value+= $btcValue;
}
}
if ( $priceData ) {
uasort($balances, function($a, $b) { return $a['btcValue'] < $b['btcValue']; });
$this->btc_value = $btc_value;
}
return $balances;
}
private function bookPriceData($array) {
$bookprices = [];
foreach ( $array as $obj ) {
$bookprices[$obj['symbol']] = [
"bid"=>$obj['bidPrice'],
"bids"=>$obj['bidQty'],
"ask"=>$obj['askPrice'],
"asks"=>$obj['askQty']
];
}
return $bookprices;
}
private function priceData($array) {
$prices = [];
foreach ( $array as $obj ) {
$prices[$obj['symbol']] = $obj['price'];
}
return $prices;
}
private function tradesData($trades) {
$output = [];
foreach ( $trades as $trade ) {
$price = $trade['p'];
$quantity = $trade['q'];
$timestamp = $trade['T'];
$maker = $trade['m'] ? 'true' : 'false';
$output[] = ["price"=>$price, "quantity"=> $quantity, "timestamp"=>$timestamp, "maker"=>$maker];
}
return $output;
}
private function http_post($url, $data, $headers=[])
{
$data = http_build_query($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
private function http_get($url, $headers=[], $data=[])
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
private function http_other($method, $url, $headers=[])
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
private function getServerTime()
{
$t = $this->time();
$t_info = json_decode($t, true);
if(isset($t_info['serverTime'])){
return $t_info['serverTime'];
}
return -1;
}
}