vampcheah/ binance-php-api
Binance API 与官方文档接口相同,支持任意扩展。
v0.0.1-beta
2021-02-14 09:09 UTC
Requires
- php: >=7.0
- guzzlehttp/guzzle: *
- workerman/globaldata: *
- workerman/workerman: *
This package is auto-updated.
Last update: 2024-09-26 03:06:18 UTC
README
建议您首先阅读官方文档
现货交易文档 https://github.com/binance-exchange/binance-official-api-docs
期货交易文档 https://binance-docs.github.io/apidocs/futures/cn
交割交易文档 https://binance-docs.github.io/apidocs/delivery/cn
支持 Websocket
大多数接口现已完成,用户可以根据我的设计继续扩展,与我一起改进。
其他交易所API
交易所 包括以下所有交易所,强烈推荐。
如果您找不到所需的交易所SDK,可以告诉我,我会加入它们。
安装
composer require linwj/binance
支持更多请求设置 更多
use Lin\Binance\Binance; $binance=new Binance($key,$secret); //You can set special needs $binance->setOptions([ //Set the request timeout to 60 seconds by default 'timeout'=>10, //https://github.com/guzzle/guzzle 'proxy'=>[], //https://php.ac.cn/manual/en/book.curl.php 'curl'=>[], //default is v1 'version'=>'v2', ]);
现货交易API
系统相关API 更多
use Lin\Binance\Binance; $binance=new Binance(); //Order book try { $result=$binance->system()->getDepth([ 'symbol'=>'BTCUSDT', 'limit'=>'20', ]); //You can set the version by passing parameters $result=$binance->system()->getDepth([ 'symbol'=>'BTCUSDT', 'limit'=>'20', ],'v2'); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } //Recent trades list try { $result=$binance->system()->getTrades([ 'symbol'=>'BTCUSDT', 'limit'=>'20', ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } //Current average price try { $result=$binance->system()->getAvgPrice([ 'symbol'=>'BTCUSDT' ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); }
交易相关API 更多
use Lin\Binance\Binance; $binance=new Binance($key,$secret); //Send in a new order. try { $result=$binance->trade()->postOrder([ 'symbol'=>'BTCUSDT', 'side'=>'BUY', 'type'=>'LIMIT', 'quantity'=>'0.01', 'price'=>'2000', 'timeInForce'=>'GTC', ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } //Check an order's status. try { $result=$binance->user()->getOrder([ 'symbol'=>'BTCUSDT', 'orderId'=>$result['orderId'], 'origClientOrderId'=>$result['origClientOrderId'], ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } //Cancel all Open Orders on a Symbol try { $result=$binance->trade()->deleteAllOrders([ 'symbol'=>'ADAUSDT', //'timeInForce'=>'GTC', ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } //Cancel an active order. try { $result=$binance->trade()->deleteOrder([ 'symbol'=>'BTCUSDT', 'orderId'=>$result['orderId'], 'origClientOrderId'=>$result['origClientOrderId'], ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } //New OCO (TRADE) try { $result=$binance->trade()->postOrderOco([ 'symbol'=>'LTCBTC', 'side'=>'SELL', 'type'=>'LIMIT', 'quantity'=>'0.1', 'price'=>'200', 'stopPrice'=>'0.1', 'timeInForce'=>'GTC', ]); print_r($result); }catch (\Exception $e){ print_r(json_decode($e->getMessage(),true)); } //Cancel OCO (TRADE) try { $result=$binance->trade()->deleteOrderList([ 'symbol'=>'LTCBTC', 'orderListId'=>'xxxxxxx', 'newClientOrderId'=>'xxxxxxx', //'listClientOrderId'=>'xxxxxxx', ]); print_r($result); }catch (\Exception $e){ print_r(json_decode($e->getMessage(),true)); } //Cancel OCO (TRADE) try { $result=$binance->trade()->deleteOrderList([ 'symbol'=>'LTCBTC', 'orderListId'=>'xxxxxxx', 'newClientOrderId'=>'xxxxxxx', //'listClientOrderId'=>'xxxxxxx', ]); print_r($result); }catch (\Exception $e){ print_r(json_decode($e->getMessage(),true)); }
用户相关API 更多
use Lin\Binance\Binance; $binance=new Binance($key,$secret); //Get all account orders; active, canceled, or filled. try { $result=$binance->user()->getAllOrders([ 'symbol'=>'BTCUSDT', 'limit'=>'20', //'orderId'=>'', //'startTime'=>'', //'endTime'=>'', ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } //Get current account information. try { $result=$binance->user()->getAccount(); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } //Query OCO (USER_DATA) try { $result=$binance->user()->getOrderList([ 'orderListId'=>'xxxxxx' ]); print_r($result); }catch (\Exception $e){ print_r(json_decode($e->getMessage(),true)); } //Query all OCO (USER_DATA) try { $result=$binance->user()->getAllOrderList(); print_r($result); }catch (\Exception $e){ print_r(json_decode($e->getMessage(),true)); } //Query Open OCO (USER_DATA) try { $result=$binance->user()->getOpenOrderList(); print_r($result); }catch (\Exception $e){ print_r(json_decode($e->getMessage(),true)); }
期货交易API
市场相关API 更多
use Lin\Binance\BinanceFuture; use Lin\Binance\BinanceDelivery; $binance=new BinanceFuture(); //Or New Delivery $binance=new BinanceDelivery(); try { $result=$binance->market()->getExchangeInfo(); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->market()->getDepth([ 'symbol'=>'BTCUSDT', 'limit'=>5 ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->market()->getTrades([ 'symbol'=>'BTCUSDT', 'limit'=>5 ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->market()->getHistoricalTrades([ 'symbol'=>'BTCUSDT' ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->market()->getAggTrades([ 'symbol'=>'BTCUSDT', 'limit'=>5 ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->market()->getPremiumIndex([ //'symbol'=>'BTCUSDT', ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->market()->getFundingRate([ 'symbol'=>'BTCUSDT', 'limit'=>5 ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); }
交易相关API 更多
use Lin\Binance\BinanceFuture; use Lin\Binance\BinanceDelivery; $binance=new BinanceFuture(); //Or New Delivery $binance=new BinanceDelivery($key,$secret); //Send in a new order. try { $result=$binance->trade()->postOrder([ 'symbol'=>'BTCUSDT', 'side'=>'BUY', 'type'=>'LIMIT', 'quantity'=>'0.01', 'price'=>'6500', 'timeInForce'=>'GTC', //'newClientOrderId'=>'xxxxxxx' ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } sleep(1); //Check an order's status. try { $result=$binance->trade()->getOrder([ 'symbol'=>'BTCUSDT', 'orderId'=>$result['orderId'], 'origClientOrderId'=>$result['clientOrderId'], ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } sleep(1); //Cancel an active order. try { $result=$binance->trade()->deleteOrder([ 'symbol'=>'BTCUSDT', 'orderId'=>$result['orderId'], 'origClientOrderId'=>$result['clientOrderId'], ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); }
用户相关API 更多
use Lin\Binance\BinanceFuture; use Lin\Binance\BinanceDelivery; $binance=new BinanceFuture(); //Or New Delivery $binance=new BinanceDelivery($key,$secret); try { $result=$binance->user()->getBalance(); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->user()->getAccount(); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->user()->getOrder([ 'symbol'=>'BTCUSDT', 'orderId'=>'111111111', 'origClientOrderId'=>'xxxxxxx', ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->user()->getOpenOrder([ 'symbol'=>'BTCUSDT', 'orderId'=>'111111111', 'origClientOrderId'=>'xxxxxxx', ]); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->user()->getLeverageBracket(); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->user()->getForceOrders(); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); } try { $result=$binance->user()->getAdlQuantile(); print_r($result); }catch (\Exception $e){ print_r($e->getMessage()); }
Websocket
Websocket有两个服务:服务器和客户端。服务器负责处理交易所的新连接、数据接收、身份验证和登录。客户端负责获取和处理数据。
服务器初始化必须在Linux CLI模式下启动。
use \Lin\Binance\BinanceWebSocket; require __DIR__ .'./vendor/autoload.php'; $binance=new BinanceWebSocket(); $binance->config([ //Do you want to enable local logging,default false 'log'=>true, //Or set the log name, //'log'=>['filename'=>'spot'], //Daemons address and port,default 0.0.0.0:2208 //'global'=>'127.0.0.1:2208', //Heartbeat time,default 20 seconds //'ping_time'=>20, //Channel subscription monitoring time,2 seconds //'listen_time'=>2, //Channel data update time,0.1 seconds //'data_time'=>0.1, //Number of messages WS queue shuold hold, default 100 //'queue_count'=>100, //baseurl 'baseurl'=>'ws://stream.binance.com:9443',//spot default //'baseurl'=>'ws://fstream.binance.com',//usdt future //'baseurl'=>'ws://dstream.binance.com',//coin future ]); $binance->start();
如果您想进行测试,可以直接执行 "php server.php start",在终端输出日志。
如果您想部署,可以执行 "php server.php start -d",启用守护进程模式,并启用 "log=>true" 以查看日志。
客户端初始化。
$binance=new BinanceWebSocket(); $binance->config([ //Do you want to enable local logging,default false 'log'=>true, //Or set the log name, //'log'=>['filename'=>'usdt-future'], //Daemons address and port,default 0.0.0.0:2208 //'global'=>'127.0.0.1:2208', //Heartbeat time,default 20 seconds //'ping_time'=>20, //Channel subscription monitoring time,2 seconds //'listen_time'=>2, //Channel data update time,0.1 seconds 'data_time'=>1, //Number of messages WS queue shuold hold, default 100 //'queue_count'=>100, //baseurl 'baseurl'=>'ws://stream.binance.com:9443',//spot default //'baseurl'=>'ws://fstream.binance.com',//usdt future //'baseurl'=>'ws://dstream.binance.com',//coin future ]);
订阅
//You can only subscribe to public channels $binance->subscribe([ 'btcusdt@depth', 'bchusdt@depth', 'btcusdt@aggTrade', 'btcusdt@trade', 'btcusdt@kline_1d', 'btcusdt@miniTicker', 'btcusdt@depth20' ]); //You can also subscribe to both private and public channels.If keysecret() is set, all private channels will be subscribed by default $binance->keysecret([ 'key'=>'xxxxxxxxx', 'secret'=>'xxxxxxxxx', ]); $binance->subscribe([ 'btcusdt@depth', 'bchusdt@depth', 'btcusdt@aggTrade', 'btcusdt@trade', 'btcusdt@kline_1d', 'btcusdt@miniTicker', 'btcusdt@depth20', ]);
取消订阅
//Unsubscribe from public channels $binance->unsubscribe([ 'btcusdt@depth', 'bchusdt@depth', 'btcusdt@aggTrade', 'btcusdt@trade', 'btcusdt@kline_1d', 'btcusdt@miniTicker', 'btcusdt@depth20' ]); //Unsubscribe from public and private channels.If keysecret() is set, all private channels will be Unsubscribed by default $binance->keysecret([ 'key'=>'xxxxxxxxx', 'secret'=>'xxxxxxxxx', ]); $binance->unsubscribe([ 'btcusdt@depth', 'bchusdt@depth', 'btcusdt@aggTrade', 'btcusdt@trade', 'btcusdt@kline_1d', 'btcusdt@miniTicker', 'btcusdt@depth20' ]);
获取所有频道订阅数据
//The first way $data=$binance->getSubscribes(); print_r(json_encode($data)); //The second way callback $binance->getSubscribes(function($data){ print_r(json_encode($data)); }); //The third way is to guard the process $binance->getSubscribes(function($data){ print_r(json_encode($data)); },true); //Note that if you need to get data in a loop, the first and second methods need to add 'pcntl_alarm(0)' while(1){ pcntl_alarm(0); sleep(1); $data=$binance->getSubscribes(); print_r(json_encode($data)); }
获取部分频道订阅数据
//The first way $data=$binance->getSubscribe([ 'btcusdt@depth', 'bchusdt@depth', ]); print_r(json_encode($data)); //The second way callback $binance->getSubscribe([ 'btcusdt@depth', 'bchusdt@depth', ],function($data){ print_r(json_encode($data)); }); //The third way is to guard the process $binance->getSubscribe([ 'btcusdt@depth', 'bchusdt@depth', ],function($data){ print_r(json_encode($data)); },true);
获取部分私有频道订阅数据
//The first way $binance->keysecret($key_secret); $data=$binance->getSubscribe();//Return all data of private channel print_r(json_encode($data)); //The second way callback $binance->keysecret($key_secret); $binance->getSubscribe([//Return all data of private channel and partial data of public channel 'btcusdt@depth', 'bchusdt@depth', ],function($data){ print_r(json_encode($data)); }); //The third way is to guard the process $binance->keysecret($key_secret); $binance->getSubscribe([//Return all data of private channel and partial data of public channel 'btcusdt@depth', 'bchusdt@depth', ],function($data){ print_r(json_encode($data)); },true);
重新链接websocket公共报价数据和私有数据
$binance->reconPublic(); $binance->reconPrivate($key);