克雷尤 / nbp-web-api
PHP的NBP Web API客户端。
v1.1.0
2023-02-08 22:22 UTC
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
简单的流畅接口,用于从NBP Web API获取数据。
此库的结构深受knplabs/github-api包的启发。
安装
此包通过HTTPlug与任何HTTP客户端解耦。
需要提供提供http-client-implementation和psr/http-factory-implementation的包。
有关更多信息,请访问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);
更改内容类型
如果您希望以XML格式而不是JSON格式检索响应,您可以在客户端更改它
use Kreyu\NBPWebApi\Client; use Kreyu\NBPWebApi\ClientInterface; $client = new Client(); $client->setContentType(ClientInterface::CONTENT_TYPE_XML);
提供HTTP客户端和工厂
默认情况下,HTTP客户端构建器使用由discovery提供的HTTP客户端和工厂。
如果您必须提供特定的PSR-18兼容客户端或任何PSR-17工厂,您可以将它们传递给HTTP客户端构建器
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);
缓存
要使用缓存机制,请向HTTP客户端构建器提供任何PSR-6兼容项池
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);
使用HTTPlug插件
您可以向HTTP客户端构建器提供任何HTTPlug插件
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);