PHP code example of itxp2008 / php-binance-api

1. Go to this page and download the library: Download itxp2008/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/ */

    

itxp2008 / php-binance-api example snippets



// 1. config in home directory
$api = new Binance\API();
// 2. config in specified file
$api = new Binance\API( "somefile.json" );
// 3. config by specifying api key and secret
$api = new Binance\API("<api key>","<secret>");
// 4. config by specifying api key, api secret and testnet flag. By default the testnet is disabled
$api = new Binance\API("<testnet api key>","<testnet secret>", true);
// 5. Rate Limiting Support
$api = new Binance\RateLimiter(new Binance\API());

$api = new Binance\API( "somefile.json" );
$api = new Binance\RateLimiter($api);
while(true) {
   $api->openOrders("BNBBTC"); // rate limited
}

$api = new Binance\API( "somefile.json" );
$api->caOverride = true;

$ticker = $api->prices();
print_r($ticker);

$price = $api->price("BNBBTC");
echo "Price of BNB: {$price} BTC.".PHP_EOL;

$api->miniTicker(function($api, $ticker) {
	print_r($ticker);
});

$ticker = $api->prices(); // Make sure you have an updated ticker object for this to work
$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->miniTicker(function($api, $ticker) {
	print_r($ticker);
});

$api->chart(["BNBBTC"], "15m", function($api, $symbol, $chart) {
	echo "{$symbol} chart update\n";
	print_r($chart);
});

$api->kline(["BTCUSDT", "EOSBTC"], "5m", function($api, $symbol, $chart) {
  //echo "{$symbol} ({$interval}) candlestick update\n";
	$interval = $chart->i;
	$tick = $chart->t;
	$open = $chart->o;
	$high = $chart->h;
	$low = $chart->l;
	$close = $chart->c;
	$volume = $chart->q; // +trades buyVolume assetVolume makerVolume
	echo "{$symbol} price: {$close}\t volume: {$volume}\n";
});

$api->trades(["BNBBTC"], function($api, $symbol, $trades) {
	echo "{$symbol} trades update".PHP_EOL;
	print_r($trades);
});

$api->ticker(false, function($api, $symbol, $ticker) {
	print_r($ticker);
});

$api->ticker("BNBBTC", function($api, $symbol, $ticker) {
	print_r($ticker);
});

$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);

//Call this before running any functions
$api->useServerTime();

$api = new Binance\API( "myfile.json" );

$api->getRequestCount();

$api->getTransfered();

$api = new Binance\API( "somefile.json" );
$api->caOverride = true;

composer 

sudo apt-get install curl php-curl
curl -s http://getcomposer.org/installer | php
php composer.phar install

php composer.phar 
composer