PHP code example of yuryblakhin / nbrb-exchange

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

    

yuryblakhin / nbrb-exchange example snippets




brb\NbrbClient;
use Nbrb\Http\CurlHttpClient;
use Nbrb\Repository\ExchangeRateRepository;
use Nbrb\Exception\NbrbException;
use Nbrb\Exception\CurrencyNotFoundException;

try {
    $client = new NbrbClient();

    $usdRate = $client->getExchangeRate(currency: 'USD');
    echo "Current USD rate: {$usdRate}\n";

    $defaultUsdRate = $client->getDefaultExchangeRate();
    echo "Current default currency (USD) rate: {$defaultUsdRate}\n";

    $eurRate = $client->getExchangeRate(currency: 'EUR', date: new DateTime(datetime: '2026-01-01'));
    echo "EUR rate on 2026-01-01: {$eurRate}\n";

    $allRates = $client->getAllExchangeRates();
    echo 'Number of currencies available: ' . count($allRates) . "\n";

    $eurClient = new NbrbClient(defaultCurrency: 'EUR');
    $defaultEurRate = $eurClient->getDefaultExchangeRate();
    echo "Current default currency (EUR) rate: {$defaultEurRate}\n";

    $httpClient = new CurlHttpClient(
        defaultOptions: [
            CURLOPT_TIMEOUT => 45,
            CURLOPT_USERAGENT => 'NBRB Client',
        ],
    );

    $repository = new ExchangeRateRepository(httpClient: $httpClient);
    $customClient = new NbrbClient(defaultCurrency: 'USD', repository: $repository);

    $usdRateCustom = $customClient->getExchangeRate(currency: 'USD');
    echo "Custom client USD rate: {$usdRateCustom}\n";
} catch (CurrencyNotFoundException $e) {
    echo 'Currency not found: ' . $e->getMessage() . "\n";
} catch (NbrbException $e) {
    echo 'Error: ' . $e->getMessage() . "\n";
}