PHP code example of binance / binance-connector-php
1. Go to this page and download the library: Download binance/binance-connector-php 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/ */
binance / binance-connector-php example snippets
composer
$client = new \Binance\Spot();
$response = $client->time();
echo json_encode($response);
$client = new \Binance\Spot(['key' => $key, 'secret' => $secret]);
$response = $client->account();
echo json_encode($response);
# RSA Key(Unencrypted) Authentication
$key = ''; # api key is also \Binance\Spot([
'key' => $key,
'privateKey' => $privateKey, # pass the key file directly
'baseURL' => 'https://testnet.binance.vision'
]);
# RSA key(Encrypted) Authentication
$key = '';
$encryptedPrivateKey = 'file:///path/to/rsa/private/key.pem';
$privateKey = openssl_pkey_get_private($encryptedPrivateKey, 'password');
$client = new \Binance\Spot([
'key' => $key,
'privateKey' => $privateKey,
'baseURL' => 'https://testnet.binance.vision'
]);
$client = new \Binance\Spot([
'baseURL' => 'https://testnet.binance.vision'
]);
$client = new \Binance\Spot(['key' => $key, 'secret' => $secret]);
$response = $client->getOrder('BNBUSDT', [
'orderId' => '11',
'recvWindow' => 10000
]
);
$response = $client->cancelOCOOrder('BNBUSDT',
[
'orderListId' => '12'
]
);
$client = new \Binance\Spot(['timeout' => 0.5]);
$response = $client->time();
echo json_encode($response);
$client = new \Binance\Spot(['showWeightUsage' => true]);
$response = $client->time();
echo json_encode($response);
$client = new \Binance\Spot(['showHeader' => true]);
$response = $client->time();
echo json_encode($response);
$client = new \Binance\Websocket\Spot();
$callbacks = [
'message' => function($conn, $msg){
echo $msg.PHP_EOL;
},
'ping' => function($conn, $msg) {
echo "received ping from server".PHP_EOL;
}
];
$client->aggTrade('btcusdt', $callbacks);
$loop = \React\EventLoop\Factory::create();
$reactConnector = new \React\Socket\Connector($loop);
$connector = new \Ratchet\Client\Connector($loop, $reactConnector);
$client = new \Binance\Websocket\Spot(['wsConnector' => $connector]);
$callbacks = [
'message' => function($conn, $msg){
echo "received message".PHP_EOL;
},
'pong' => function($conn) {
echo "received pong from server".PHP_EOL;
},
'ping' => function($conn) {
echo "received ping from server".PHP_EOL;
},
'close' => function($conn) {
echo "receive closed.".PHP_EOL;
}
];
$client->miniTicker('btcusdt', $callbacks);
# send ping to server intervally
$loop->addPeriodicTimer(2, function () use ($client) {
$client->ping();
echo "ping sent ".PHP_EOL;
});
$loop->run();
$client->combined([
'btcusdt@miniTicker',
'ethusdt@miniTicker'
], $callbacks);