Simple and fluent interface to retrieve data from NBP Web API.
This library structure is strongly inspired by knplabs/github-api package.
This package is decoupled from any HTTP client thanks to the HTTPlug.
Requires packages that provide http-client-implementation and psr/http-factory-implementation.
For more information visit HTTPlug for library users.
$ composer require kreyu/nbp-web-api
use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\Api\ExchangeRates;
$client = new Client();
// Example call
$client->exchangeRates(ExchangeRates::TABLE_TYPE_A)->forCurrency('EUR')->latest(5);
If you wish to retrieve responses in XML format rather than JSON, you can change it in the client:
use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\ClientInterface;
$client = new Client();
$client->setContentType(ClientInterface::CONTENT_TYPE_XML);
By default, the HTTP client builder uses HTTP client and factories provided by the discovery.
If you have to provide specific PSR-18 compliant client or any PSR-17 factory, you can pass them to the HTTP client builder:
use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\Http\ClientBuilder as HttpClientBuilder;
/**
* @var $client Psr\Http\Client\ClientInterface
* @var $requestFactory Psr\Http\Message\RequestFactoryInterface
* @var $streamFactory Psr\Http\Message\StreamFactoryInterface
* @var $uriFactory Psr\Http\Message\UriFactoryInterface
*/
$builder = new HttpClientBuilder(
$client,
$requestFactory,
$streamFactory,
$uriFactory
);
$client = new Client($builder);
To use caching mechanism, provide any PSR-6 compliant item pool to the HTTP client builder:
use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\Http\ClientBuilder as HttpClientBuilder;
/** @var $pool Psr\Cache\CacheItemPoolInterface */
$builder = new HttpClientBuilder();
$builder->setCache($pool);
$client = new Client($builder);
You can provide any HTTPlug plugin to the HTTP client builder:
use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\Http\ClientBuilder as HttpClientBuilder;
/** @var $plugin Http\Client\Common\Plugin */
$builder = new HttpClientBuilder();
$builder->addPlugin($plugin);
$client = new Client($builder);