PHP code example of jlcd / php-binance-api
1. Go to this page and download the library: Download jlcd/php-binance-api 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/ */
jlcd / php-binance-api example snippets
$api = new Binance\API("<api key>","<secret>");
$ticker = $api->prices();
print_r($ticker); // List prices of all symbols
echo "Price of BNB: {$ticker['BNBBTC']} BTC.".PHP_EOL;
$balances = $api->balances($ticker);
print_r($balances);
echo "BTC owned: ".$balances['BTC']['available'].PHP_EOL;
echo "ETH owned: ".$balances['ETH']['available'].PHP_EOL;
echo "Estimated Value: ".$api->btc_value." BTC".PHP_EOL;
$bookPrices = $api->bookPrices();
print_r($bookPrices);
echo "Bid price of BNB: {$bookPrices['BNBBTC']['bid']}".PHP_EOL;
$quantity = 1;
$price = 0.0005;
$order = $api->buy("BNBBTC", $quantity, $price);
$quantity = 1;
$price = 0.0006;
$order = $api->sell("BNBBTC", $quantity, $price);
$quantity = 1;
$order = $api->marketBuy("BNBBTC", $quantity);
$quantity = 0.01;
$order = $api->marketSell("ETHBTC", $quantity);
// When the stop is reached, a stop order becomes a market order
$type = "STOP_LOSS"; // Set the type STOP_LOSS (market) or STOP_LOSS_LIMIT, and TAKE_PROFIT (market) or TAKE_PROFIT_LIMIT
$quantity = 1;
$price = 0.5; // Try to sell it for 0.5 btc
$stopPrice = 0.4; // Sell immediately if price goes below 0.4 btc
$order = $api->sell("BNBBTC", $quantity, $price, $type, ["stopPrice"=>$stopPrice]);
print_r($order);
// Iceberg orders are intended to conceal the true order quantity.
$type = "LIMIT"; // LIMIT, STOP_LOSS_LIMIT, and TAKE_PROFIT_LIMIT
$quantity = 1;
$price = 0.5;
$icebergQty = 10;
$order = $api->sell("BNBBTC", $quantity, $price, $type, ["icebergQty"=>$icebergQty]);
print_r($order);
$prevDay = $api->prevDay("BNBBTC");
print_r($prevDay);
echo "BNB price change since yesterday: ".$prevDay['priceChangePercent']."%".PHP_EOL;
$history = $api->history("BNBBTC");
print_r($history);
$depth = $api->depth("ETHBTC");
print_r($depth);
$openorders = $api->openOrders("BNBBTC");
print_r($openorders);
$orderid = "7610385";
$orderstatus = $api->orderStatus("ETHBTC", $orderid);
print_r($orderstatus);
$response = $api->cancel("ETHBTC", $orderid);
print_r($response);
$trades = $api->aggTrades("BNBBTC");
print_r($trades);
$orders = $api->orders("BNBBTC");
print_r($orders);
//Periods: 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
$ticks = $api->candlesticks("BNBBTC", "5m");
print_r($ticks);
$api->chart(["BNBBTC"], "15m", function($api, $symbol, $chart) {
echo "{$symbol} chart update\n";
print_r($chart);
});
$api->trades(["BNBBTC"], function($api, $symbol, $trades) {
echo "{$symbol} trades update".PHP_EOL;
print_r($trades);
});
$api->depthCache(["BNBBTC"], function($api, $symbol, $depth) {
echo "{$symbol} depth cache update".PHP_EOL;
//print_r($depth); // Print all depth data
$limit = 11; // Show only the closest asks/bids
$sorted = $api->sortDepth($symbol, $limit);
$bid = $api->first($sorted['bids']);
$ask = $api->first($sorted['asks']);
echo $api->displayDepth($sorted);
echo "ask: {$ask}".PHP_EOL;
echo "bid: {$bid}".PHP_EOL;
});
$balance_update = function($api, $balances) {
print_r($balances);
echo "Balance update".PHP_EOL;
};
$order_update = function($api, $report) {
echo "Order update".PHP_EOL;
print_r($report);
$price = $report['price'];
$quantity = $report['quantity'];
$symbol = $report['symbol'];
$side = $report['side'];
$orderType = $report['orderType'];
$orderId = $report['orderId'];
$orderStatus = $report['orderStatus'];
$executionType = $report['orderStatus'];
if ( $executionType == "NEW" ) {
if ( $executionType == "REJECTED" ) {
echo "Order Failed! Reason: {$report['rejectReason']}".PHP_EOL;
}
echo "{$symbol} {$side} {$orderType} ORDER #{$orderId} ({$orderStatus})".PHP_EOL;
echo "..price: {$price}, quantity: {$quantity}".PHP_EOL;
return;
}
//NEW, CANCELED, REPLACED, REJECTED, TRADE, EXPIRED
echo "{$symbol} {$side} {$executionType} {$orderType} ORDER #{$orderId}".PHP_EOL;
};
$api->userData($balance_update, $order_update);
$asset = "BTC";
$address = "1C5gqLRs96Xq4V2ZZAR1347yUCpHie7sa";
$amount = 0.2;
$response = $api->withdraw($asset, $address, $amount);
print_r($response);
//Required for coins like XMR, XRP, etc.
$address = "44tLjmXrQNrWJ5NBsEj2R77ZBEgDa3fEe9GLpSf2FRmhexPvfYDUAB7EXX1Hdb3aMQ9FLqdJ56yaAhiXoRsceGJCRS3Jxkn";
$addressTag = "0e5e38a01058dbf64e53a4333a5acf98e0d5feb8e523d32e3186c664a9c762c1
";
$amount = 0.1;
$response = $api->withdraw($asset, $address, $amount, $addressTag);
print_r($response);
$withdrawHistory = $api->withdrawHistory();
print_r($withdrawHistory);
$withdrawHistory = $api->withdrawHistory("BTC");
print_r($withdrawHistory);
$depositAddress = $api->depositAddress("VEN");
print_r($depositAddress);
$depositHistory = $api->depositHistory();
print_r($depositHistory);
composer
sudo apt-get install curl
curl -s http://getcomposer.org/installer | php
php composer.phar install
php composer.phar