PHP code example of ivashchukk / pkbp-rates

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

    

ivashchukk / pkbp-rates example snippets


use Ivashchukk\PkbpRates\Currency;
use Ivashchukk\PkbpRates\PkbpRatesClient;

$client = PkbpRatesClient::create();

$conversion = $client->convert(
    10_000,          // 100.00 DKK
    Currency::DKK,
    Currency::EUR,
);

echo $conversion->source->decimal(); // 100.00
echo $conversion->target->decimal(); // 13.21
echo $conversion->rate;              // EUR per 1 DKK, as a decimal string

use Ivashchukk\PkbpRates\Currency;
use Ivashchukk\PkbpRates\Dto\Money;

$money = Money::ofDecimal('100.00', Currency::DKK);

$conversion = $client->convertMoney($money, Currency::EUR);

echo $conversion->target->minorAmount; // integer cents
echo $conversion->target->decimal();   // display value

$eur = $client->convert(10_000, Currency::PLN, Currency::EUR); // 100.00 PLN -> EUR
$pln = $client->convert(1_000, Currency::EUR, Currency::PLN);  // 10.00 EUR -> PLN

$rate = $client->conversionRate(Currency::DKK, Currency::EUR);

echo $rate; // decimal string, not a float

use Ivashchukk\PkbpRates\RateKind;

$eur = $client->sellRate(Currency::EUR);

echo $eur->rate; // PLN per 1 EUR, decimal string

$averageUsd = $client->rate(
    Currency::USD,
    '2024-06-14',
    RateKind::AverageForeign,
);

$client->convert(10_000, Currency::DKK, Currency::EUR);

$client->convert(10_000, 'DKK', 'EUR');

$currency = Currency::fromCode('eur'); // Currency::EUR

$conversion = $client->convert(
    10_000,
    Currency::DKK,
    Currency::EUR,
    '2024-06-14',
);

use Ivashchukk\PkbpRates\Exception\PkbpRatesException;

try {
    $conversion = $client->convert(10_000, Currency::DKK, Currency::EUR);
} catch (PkbpRatesException $exception) {
    // HTTP error, invalid PKO response, missing table, or missing currency.
}

use Ivashchukk\PkbpRates\PkbpRatesClient;

$client = new PkbpRatesClient($psr18Client, $psr17RequestFactory);