kreyu / nbp-web-api
NBP Web API client for PHP.
Installs: 4 395
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- php-http/cache-plugin: ^1.7
- php-http/client-common: ^2.2
- php-http/discovery: ^1.11
- php-http/httplug: ^2.0
- psr/cache: ^1.0 || ^2.0 || ^3.0
- psr/http-client-implementation: ^1.0
- psr/http-factory-implementation: ^1.0
- psr/http-message: ^1.0
- webmozart/assert: ^1.0
Requires (Dev)
- ext-dom: *
- ext-mbstring: *
- guzzlehttp/psr7: ^1.2
- http-interop/http-factory-guzzle: ^1.0
- php-http/guzzle6-adapter: ^1.0 || ^2.0
- phpunit/phpunit: ^8.2.3
README
Simple and fluent interface to retrieve data from NBP Web API.
This library structure is strongly inspired by knplabs/github-api package.
Installation
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
Usage
use Kreyu\NBPWebApi\Client; use Kreyu\NBPWebApi\Api\ExchangeRates; $client = new Client(); // Example call $client->exchangeRates(ExchangeRates::TABLE_TYPE_A)->forCurrency('EUR')->latest(5);
Changing content type
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);
Providing HTTP client and factories
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);
Caching
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);
Using HTTPlug plugins
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);