Retrieve average currency exchange rates from the NBP website and convert an amount from one currency to another.
Via Composer
$ composer require ksdev/nbp-currency-converterIf optional table publication date is not passed to convert or averageExchangeRates methods, the last published
average exchange rates table will be used, starting from yesterday, so e.g. on 2015-07-10 (friday) a table from
2015-07-09 (thursday) will be used, and on 2015-07-12 (sunday) a table from 2015-07-10 (friday) - unless no table was
published on this day, then the table from previous day will be used, etc.
use Ksdev\NBPCurrencyConverter\CurrencyConverter;
use Ksdev\NBPCurrencyConverter\ExRatesTableFinder;
use Ksdev\NBPCurrencyConverter\ExRatesTableFactory;
use GuzzleHttp\Client;
$converter = new CurrencyConverter(
new ExRatesTableFinder(
new Client(),
new ExRatesTableFactory(),
'path/to/cache/folder'
)
);
try {
$result = $converter->convert('123.4567', 'PLN', 'USD');
$avgExRates = $converter->averageExchangeRates();
}
catch (Exception $e) {
//
}array(
'publication_date' => '2015-07-03',
'amount' => '32.7246',
'currency' => 'USD'
);array(
'numer_tabeli' => '127/A/NBP/2015',
'data_publikacji' => '2015-07-03',
'waluty' =>
array(
'PLN' =>
array(
'nazwa_waluty' => 'złoty polski',
'przelicznik' => '1',
'kurs_sredni' => '1',
),
'THB' =>
array(
'nazwa_waluty' => 'bat (Tajlandia)',
'przelicznik' => '1',
'kurs_sredni' => '0,1117',
),
'USD' =>
array(
'nazwa_waluty' => 'dolar amerykański',
'przelicznik' => '1',
'kurs_sredni' => '3,7726',
),
'AUD' =>
array(...)
)
);$ composer testThe MIT License (MIT). Please see License File for more information.