-
Notifications
You must be signed in to change notification settings - Fork 65
/
binance.php
417 lines (356 loc) · 10.3 KB
/
binance.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**
* @author lin <465382251@qq.com>
*
* Most of them are unfinished and need your help
* https://github.com/zhouaini528/binance-php.git
* */
use Lin\Exchange\Exchanges;
require __DIR__ .'../../vendor/autoload.php';
include 'key_secret.php';
$key=$keysecret['binance']['key'];
$secret=$keysecret['binance']['secret'];
$spot_host='https://api.binance.com';
$future_host='https://fapi.binance.com';
$exchanges=new Exchanges('binance',$key,$secret,$spot_host);
//Support for more request Settings
$exchanges->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,
//If you are developing locally and need an agent, you can set this
//'proxy'=>true,
//More flexible Settings
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//Close the certificate
//'verify'=>false,
]);
$action=intval($_GET['action'] ?? 0);//http pattern
if(empty($action)) $action=intval($argv[1]);//cli pattern
switch ($action){
//***********system error
//exception testing
case 1:{
$result=$exchanges->trader()->buy([
'_symbol'=>'exception testing',
'_number'=>'0.01',
]);
/*
return Array
(
[_error] => Array
(
[code] => -1100
[msg] => Illegal characters found in parameter 'symbol'; legal range is '^[A-Z0-9_]{1,20}$'.
[_method] => POST
[_url] => https://api.binance.com/api/v3/order
[_httpcode] => 400
)
)
*/
break;
}
case 2:{
//If you are developing locally and need an agent, you can set this
$exchanges->setOptions([
'proxy'=>true,
]);
//More flexible Settings
$exchanges->setOptions([
'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
],
]);
//Get current account information.
$result=$exchanges->account()->get();
break;
}
//******************************Spot
//***********Spot Market
case 100:{
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'10',
]);
break;
}
case 101:{
//The original parameters
$result=$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'type'=>'MARKET',
'quantity'=>'0.01',
]);
break;
}
case 102:{
$result=$exchanges->trader()->sell([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
]);
break;
}
case 103:{
//The original parameters
$result=$exchanges->trader()->sell([
'symbol'=>'BTCUSDT',
'type'=>'MARKET',
'quantity'=>'0.01',
]);
break;
}
//***********Spot Limit
case 150:{
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
'_price'=>'2000',
]);
break;
}
case 151:{
//The original parameters
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->buy([
'newClientOrderId'=>$_client_id,
'symbol'=>'BTCUSDT',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'2000',
'timeInForce'=>'GTC',
]);
break;
}
case 152:{
$result=$exchanges->trader()->sell([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
'_price'=>'9000',
]);
break;
}
case 153:{
//The original parameters
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->sell([
'newClientOrderId'=>$_client_id,
'symbol'=>'BTCUSDT',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'9000',
'timeInForce'=>'GTC',
]);
break;
}
case 300:{
$result=$exchanges->trader()->show([
'_symbol'=>'ETHUSDT',
//'_order_id'=>'324314658',
//'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
'_order_id'=>'4135060190',
'_client_id'=>'b383abd88086f623e762c35727455b9a',
]);
break;
}
case 301:{
$result=$exchanges->trader()->show([
'symbol'=>'BTCUSDT',
'orderId'=>'324317395',
//'origClientOrderId'=>'',
]);
break;
}
case 302:{
$result=$exchanges->trader()->cancel([
'_symbol'=>'BTCUSDT',
'_order_id'=>'324314658',
]);
break;
}
case 303:{
$result=$exchanges->trader()->cancel([
'symbol'=>'BTCUSDT',
'orderId'=>'324317395',
//'origClientOrderId'=>'',
]);
break;
}
case 304:{
//Get current account information.
$result=$exchanges->account()->get();
break;
}
//******************************Complete spot flow
case 400:{
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->buy([
'_symbol'=>'ETHUSDT',
'_number'=>'0.01',
'_price'=>'1000',
'_client_id'=>$_client_id,
]);
print_r($result);
$result=$exchanges->trader()->cancel([
'_symbol'=>'ETHUSDT',
//'_order_id'=>$result['orderId'],
'_client_id'=>$_client_id,
//'_client_id'=>'cd0986e64b3faca04724b82a3ca279f2'
]);
break;
}
//******************************Complete future flow
case 450:{
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.001',
'_price'=>'6500',
]);
print_r($result);
$result=$exchanges->trader()->cancel([
'_symbol'=>'BTCUSDT',
'_order_id'=>$result['orderId'],
//'_client_id'=>$_client_id,
]);
break;
}
case 461:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->account()->get();
break;
}
case 462:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.001',
]);
break;
}
case 463:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'quantity'=>'0.001',
'type'=>'LIMIT',
'price'=>'6500',
'timeInForce'=>'GTC',
]);
break;
}
case 463:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'quantity'=>'0.001',
'type'=>'LIMIT',
'price'=>'6500',
'timeInForce'=>'GTC',
]);
break;
}
case 464:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->trader()->show([
'_symbol'=>'BTCUSDT',
'_order_id'=>'487693783',
]);
break;
}
case 1001:{
//Public API
$exchanges=new Exchanges('binance');
$exchanges->setOptions([
'timeout'=>10,
'proxy'=>true,
'verify'=>false,
]);
$result=$exchanges->getPlatform()->system()->getDepth([
'symbol'=>'BTCUSDT',
]);
break;
}
case 4000:{
$_client_id=md5(rand(1,999999999));//custom ID
$exchanges->setPlatform('spot')->setVersion('v1');
$result=$exchanges->trader()->buy([
'_symbol'=>'ETHUSDT',
'_number'=>'0.01',
'_price'=>'1000',
'_client_id'=>$_client_id,
]);
print_r($result);
$result=$exchanges->trader()->cancel([
'_symbol'=>'ETHUSDT',
//'_order_id'=>$result['orderId'],
'_client_id'=>$_client_id,
//'_client_id'=>'cd0986e64b3faca04724b82a3ca279f2'
]);
break;
}
case 4001:{
//Send in a new order.
try {
$result=$exchanges->getPlatform('spot')->trade()->postOrder([
'symbol'=>'ETHUSDT',
'side'=>'BUY',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'1000',
'timeInForce'=>'GTC',
]);
print_r($result);
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
//Check an order's status.
try {
$result=$exchanges->getPlatform('spot')->user()->getOrder([
'symbol'=>'ETHUSDT',
'orderId'=>$result['orderId'],
]);
print_r($result);
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
//Cancel an active order.
try {
$result=$exchanges->getPlatform('spot')->trade()->deleteOrder([
'symbol'=>'ETHUSDT',
'orderId'=>$result['orderId'],
//'origClientOrderId'=>'1111111',
]);
print_r($result);
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
break;
}
case 4002:{
$_client_id=md5(rand(1,999999999));//custom ID
$exchanges->setPlatform('future')->setVersion('v1');
$result=$exchanges->trader()->buy([
'_symbol'=>'ETHUSDT',
'_number'=>'0.01',
'_price'=>'1000',
'_client_id'=>$_client_id,
]);
print_r($result);
$result=$exchanges->trader()->cancel([
'_symbol'=>'ETHUSDT',
//'_order_id'=>$result['orderId'],
'_client_id'=>$_client_id,
//'_client_id'=>'cd0986e64b3faca04724b82a3ca279f2'
]);
break;
}
default:{
echo 'nothing';
exit;
}
}
print_r($result);