1. Go to this page and download the library: Download linwj/binance library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
linwj / binance example snippets
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://www.php.net/manual/en/book.curl.php
'curl'=>[],
//default is v1
'version'=>'v2',
]);
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());
}
use \Lin\Binance\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();
$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);