API for accessing Polish National Bank (NBP - Narodowy Bank Polski) currency and commodities exchange rates.
composer require maciej-sz/nbp-php
The full API of this library can be found in api.md.
<?php
require_once 'vendor/autoload.php';
use MaciejSz\Nbp\Service\CurrencyAverageRatesService;
$currencyAverages = CurrencyAverageRatesService::new();
$rate = $currencyAverages->fromDay('2023-01-02')->fromTable('A')->getRate('USD');
printf('%s rate is %d', $rate->getCurrencyCode(), $rate->getValue());
USD rate is 4.381100
See the examples directory for complete and runnable examples.
Note that a library implementing PSR-6 has to be provided in order to use the caching abilities.
The CachedTransport
class is a proxy for all other transport implementations.
This transport has to be backed by another transport, as it relies on it to
make the actual requests that have not been cached yet.
use Symfony\Component\Cache\Adapter\FilesystemAdapter as CachePoolAdapter;
// 1) create repository backed by caching transport
$cachePool = new CachePoolAdapter();
$cachingTransportFactory = CachingTransportFactory::new($cachePool);
$client = NbpWebClient::new(transportFactory: $cachingTransportFactory);
$nbpRepository = NbpWebRepository::new($client);
// 2) create needed services using cache-backed repository:
$goldRates = new GoldRatesService($nbpRepository);
// 3) run multiple times to check the effect of caching:
$start = microtime(true);
$goldRates->fromDayBefore('2013-05-15')->getValue();
$end = microtime(true);
$took = $end - $start;
printf('Getting the rate took %F ms', $took * 1000);
The library uses Symfony HTTP Client and Guzzle as default transports.
If those packages are not available then it falls back to the file_get_contents
method. This may not be ideal in some situations. Especially when there is no access
to HTTP client packages as may be the case when using PHP version prior to 8.0.
In such cases it is suggested to use different transport. It can be achieved
by replacing the TransportFactory
of the NbpClient
with your own implementation.
$customTransportFactory = new class() implements TransportFactory {
public function create(string $baseUri): Transport
{
return new class() implements Transport {
public function get(string $path): array
{
echo "Requesting resource: {$path}" . PHP_EOL;
$ch = curl_init();
$url = NbpWebClient::BASE_URL . $path;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
try {
$output = curl_exec($ch);
} finally {
curl_close($ch);
}
echo 'Request successful' . PHP_EOL;
return json_decode($output, true);
}
};
}
};
$client = NbpWebClient::new(transportFactory: $customTransportFactory);
$nbpRepository = NbpWebRepository::new($client);
$goldRates = GoldRatesService::new($nbpRepository);
$rate = $goldRates->fromDay('2022-01-03');
printf(
'Gold rate from %s is %F',
$rate->getDate()->format('Y-m-d'),
$rate->getValue()
);
Requesting resource: /api/cenyzlota/2022-01-01/2022-01-31
Request successful
Gold rate from 2022-01-03 is 235.720000
On the infrastructure level the code utilizes layered architecture to keep the concerns separated.
The service consists of facades
for the package. Classes here are named services
instead of facades due to common
understanding of this word as something through which you access the internals of a system.
This layer provides a high level business-oriented methods for interacting
with the NBP API.
It exposes most common use cases of the nbp-php
package and is the
likely starting point for all applications using this package.
One needs to interact with other layers only for more complex tasks.
The service layer is structured in the way that it directly communicates with the repository layer.
Repository layer allows getting data from NBP API by providing methods that closely reflect the NBP Web API. This layer operates on higher level, hydrated domain objects.
The only constraint here is that the repository layer operates on month-based dates. So for example you can get trading rates from entire month, but in order to retrieve a specific date you have to iterate through the retrieved month (you can use service layer for that). This is by design for the purpose of reducing network traffic to the NBP servers. It also allows simplifying caching on the transport layer, because there is no need for any cache-pooling logic.
Client layer is a bridge between the repository and the transport layer. It processes request objects on the input, and then it uses the transport layer to fulfill those requests.
The requests thet client layer uses are higher-level requests then those on
the transport layer. They implement NbpClientRequest
interface.
The transport layer is responsible for directly interacting with the NBP API. A few independent transport implementations are provided for serving connections to the NBP API.
It is also equipped with a convenient factories which pick the most appropriate implementation depending on installed libraries and configuration.