PHP code example of rekryt / tbank-lib

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

    

rekryt / tbank-lib example snippets


$client = Client::create(new ClientConfig(token: $token));

$portfolio = $client->operations()->getPortfolio($accountId);

echo $portfolio->totalAmountPortfolio?->toString();   // 101405.12 RUB



declare(strict_types=1);

use OpenCCK\Client;
use OpenCCK\ClientConfig;

'));

$accounts = $client->users()->getAccounts();
$accountId = $accounts->accounts[0]->id;

$portfolio = $client->operations()->getPortfolio($accountId);

printf("Счёт %s: %s\n", $accountId, $portfolio->totalAmountPortfolio?->toString() ?? '—');

foreach ($portfolio->positions as $position) {
    printf(
        "  %-12s %s шт., доходность %s\n",
        $position->figi,
        $position->quantity?->toString() ?? '0',
        $position->expectedYield?->toString() ?? '0'
    );
}

$stream = $client->marketDataStream();

$stream->subscribeCandles([$instrumentUid], SubscriptionInterval::OneMinute);
$stream->subscribeLastPrice([$instrumentUid]);

foreach ($stream->events() as $event) {
    if ($event instanceof PayloadEvent && $event->payload instanceof Candle) {
        $candle = $event->payload;
        printf("%s: закрытие %s\n", $candle->time?->toString() ?? '', $candle->close?->toString() ?? '');
    }

    // за время разрыва биржа торговала: пропуск добирают unary-методами
    if ($event instanceof StreamDisconnectedEvent) {
        printf("разрыв, попытка %d через %.1f с\n", $event->attempt, $event->retryIn);
    }
}

$registry = $client->registry();

$registry->warmup();                       // весь список одним запросом
$sber = $registry->byTicker('SBER', 'TQBR');

echo $sber?->uid;
sh
php examples/quickstart/first-request.php        # счета и портфель
php examples/market-data/get-candles.php         # один метод API
php examples/streams/market-data-stream.php      # стрим котировок