PHP code example of kleninm / binance-api

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

    

kleninm / binance-api example snippets


   $binance->depth($symbol, $limit);
   

   $binance->tickerBookTicker($symbol);
   

   $binance->ticker24hr($symbol, $symbols, $type);
   

   $binance->capitalWithdrawHistory();
   

 $binance = new \BinanceApi\Binance();

 $fullResult = $binance->depth('BTCUSDT', 2);

 $orderbook = $fullResult['response']['data'];
 

$binanceTestNet = new \BinanceApi\Binance(TestNet::BASE_ENDPOINT);

$fullResult = $binance->depth('BTCUSDT', 2);

$binance->setApiKeys($apiKey, $secretKey);

try {
    $result = $binance->depth('BTCUSDT', 2);
} catch (BinanceApi\Exception\BinanceResponseException $e) {
    // This is exception throw, when binance return error message
    // https://binance-docs.github.io/apidocs/spot/en/#error-codes
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // It's about Guzzle exception
    // https://docs.guzzlephp.org/en/stable/quickstart.html#exceptions
}

$binance->getAdditional()['limits']['IP']['api'];

$dateTime = new DateTime($binance->getAdditional()['limits']['IP']['api']['lastRequest']);

$dateTime = new DateTime($binance->getAdditional()['limits']['IP']['sapi']['lastRequest']);

\BinanceApi\Docs\GeneralInfo\Signed::binanceMicrotime(); //1690113560956

$binance = new \BinanceApi\Binance();

$binance->setApiKeys($apiKey, $secretKey);

// Filter every output. Read more about it in a Basic topic or just use it if you need only a body result from request
$binance->setOutputCallback(function ($output) {
    return $output['response']['data'];
});

$binance->exchangeInfo();

$binance->depth('BTCUSDT', 5);

$binance->orderbook('BTCUSDT', 5);

$binance->orderbookBTCUSDT(5); // "BTCUSDT" you can replace with any market: "ETHUSDT", "BTCBUSD", ...

$binance->trades('BTCUSDT', 5);

$binance->tradesETHUSDT(5); // "ETHUSDT" you can replace with any market: "BTCUSDT", "BTCBUSD", ...

$binance->klines('BTCUSDT', '1m', limit: 50);

$startTime = (new DateTime('01 Jan 2022 00:00:00 GMT'))->getTimestamp() * 1000;
$binance->klines('BTCUSDT', '1d', $startTime);

$endTime = (new DateTime('01 Jan 2023 00:00:00 GMT'))->getTimestamp() * 1000;
$binance->klines('BTCUSDT', '1d', endTime: $endTime);

$binance->secondKlines('BTCUSDT');

$binance->minuteKlines('BTCUSDT');

$binance->threeMinuteKlines('BTCUSDT');

$binance->fiveMinuteKlines('BTCUSDT');

$binance->fifteenMinuteKlines('BTCUSDT');

$binance->thirtyMinuteKlines('BTCUSDT');

$binance->hourKlines('BTCUSDT');

$binance->twoHourKlines('BTCUSDT');

$binance->fourHourKlines('BTCUSDT');

$binance->sixHourKlines('BTCUSDT');

$binance->eightHourKlines('BTCUSDT');

$binance->twelveHourKlines('BTCUSDT');

$binance->dayKlines('BTCUSDT');

$binance->threeDayKlines('BTCUSDT');

$binance->weekKlines('BTCUSDT');

$binance->monthKlines('BTCUSDT');

$startTime = new DateTime('01 Jan 2022 00:00:00 GMT');
$binance->hourKlines('BTCUSDT', $startTime, limit: 24);

$endTime = new DateTime('01 Jan 2022 00:00:00 GMT');
$binance->hourKlines('BTCUSDT', endTime: $endTime, limit: 48);

$binance->tickerPrice();

$binance->tickerPrice('BTCUSDT');

$binance->order('BTCUSDT', 'BUY', 'LIMIT', 'GTC', 0.01, price: 20000);

$binance->limitOrder('BTCUSDT', 'BUY', 0.01, price: 21000);

$binance->order('BTCUSDT', 'BUY', 'MARKET', quantity: 0.01);

$binance->marketOrder('BTCUSDT', 'SELL', 0.01);

$binance->order('BTCUSDT', 'SELL', 'STOP_LOSS', 'GTC', 0.01, stopPrice: 25000);

$binance->stopLossOrder('BTCUSDT', 'SELL', 0.01, stopPrice: 25000);

$binance->order('BTCUSDT', 'SELL', 'TAKE_PROFIT', 'GTC', 0.01, stopPrice: 100000);

$binance->takeProfitOrder('BTCUSDT', 'SELL', 0.01, stopPrice: 100000);

$binance->openOrders('BTCUSDT');

$binance->openOrders();

$binance->getOrder('BTCUSDT', 8403075);

$binance->cancelOrder('BTCUSDT', 8403075);

$binance->cancelOpenOrders('BTCUSDT');

$binance->account();

$binance->myTrades('BTCUSDT');

$binance->capitalConfigGetall();

$binance->capitalWithdrawApply('USDT', network: 'TRX', address: 'TNGjavWm7sMjCA4r1YhsEYGfaZtZEkXzNf', amount: 10);

$binance->withdraw('USDT', network: 'TRX', address: 'TNGjavWm7sMjCA4r1YhsEYGfaZtZEkXzNf', amount: 10);

$binance->capitalWithdrawHistory();

$binance->capitalDepositAddress('USDT', 'TRX');

$binance->capitalDepositHisrec();