nelexa / http-client-simple-cache
基于Guzzle的HTTP客户端,具有自定义处理HTTP请求结果的缓存功能(不基于HTTP头部)。
1.1.1
2021-02-25 20:03 UTC
Requires
- php: ^7.1|^8.0
- guzzlehttp/guzzle: ^6.5
- psr/simple-cache: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.0|^8.0|^9.0
- symfony/cache: ^4.4|^5.0
- symfony/var-dumper: ^4.4|^5.0
Suggests
- symfony/cache: Install the implementation of the simple cache PSR-16. Full list of packages https://packagist.org.cn/providers/psr/simple-cache-implementation
This package is auto-updated.
Last update: 2024-08-26 04:13:56 UTC
README
基于Guzzle的HTTP客户端,具有自定义处理HTTP请求结果的缓存功能(不基于HTTP头部)。
文档
Guzzle文档: http://docs.guzzlephp.org/en/stable/
初始化
<?php $client = new \Nelexa\HttpClient\HttpClient();
设置默认选项
<?php $client = new \Nelexa\HttpClient\HttpClient([ \Nelexa\HttpClient\Options::HEADERS => [ 'User-Agent' => 'TestHttpClient/1.0', ], ]);
处理HTTP请求并获取结果
<?php $client = new \Nelexa\HttpClient\HttpClient(); $result = $client->get($url, [ \Nelexa\HttpClient\Options::HANDLER_RESPONSE => $callable ]);
可调用签名
function(\Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response){ return ...; }
示例
<?php $client = new \Nelexa\HttpClient\HttpClient(); // use \Closure handler $base64Contents = $client->get($url, [ \Nelexa\HttpClient\Options::HANDLER_RESPONSE => static function (Psr\Http\Message\RequestInterface $request, Psr\Http\Message\ResponseInterface $response) { return base64_encode($response->getBody()->getContents()); }, ]); // or use class handler $base64Contents = $client->get($url, [ \Nelexa\HttpClient\Options::HANDLER_RESPONSE => new class() implements \Nelexa\HttpClient\ResponseHandlerInterface { public function __invoke(Psr\Http\Message\RequestInterface $request, Psr\Http\Message\ResponseInterface $response) { return base64_encode($response->getBody()->getContents()); } }, ]);
使用缓存结果
安装简单缓存PSR-16的实现。
包列表 https://packagist.org.cn/providers/psr/simple-cache-implementation
安装示例
composer require symfony/cache
添加选项 \Nelexa\HttpClient\Options::CACHE_TTL
并使用 \DateInterval
值。
示例
<?php class Api { /** @var \Nelexa\HttpClient\HttpClient */ private $httpClient; public function __construct(Psr\SimpleCache\CacheInterface $cache) { $this->httpClient = new \Nelexa\HttpClient\HttpClient([], $cache); } /** * Fetch uuid. * * @return string */ public function fetchUUID(): string { return $this->httpClient->request('GET', 'https://httpbin.org/uuid', [ \Nelexa\HttpClient\Options::CACHE_TTL => \DateInterval::createFromDateString('1 min'), // required TTL \Nelexa\HttpClient\Options::HANDLER_RESPONSE => static function (Psr\Http\Message\RequestInterface $request, Psr\Http\Message\ResponseInterface $response) { $contents = $response->getBody()->getContents(); $json = \GuzzleHttp\json_decode($contents, true); return $json['uuid']; }, ]); } } $cache = new \Symfony\Component\Cache\Psr16Cache( new \Symfony\Component\Cache\Adapter\RedisAdapter( \Symfony\Component\Cache\Adapter\RedisAdapter::createConnection('redis://localhost') ) ); $api = new Api($cache); $UUID = $api->fetchUUID(); \PHPUnit\Framework\Assert::assertSame($api->fetchUUID(), $UUID); var_dump($UUID); // string(36) "a72b27c2-7e69-4bc8-8d5b-ccb0e496a7bf"
异步请求池处理器
<?php $urls = [ 'jpeg' => 'https://httpbin.org/image/jpeg', 'png' => 'https://httpbin.org/image/png', 'webp' => 'https://httpbin.org/image/webp', ]; $client = new \Nelexa\HttpClient\HttpClient(); $result = $client->requestAsyncPool('GET', $urls, [ \Nelexa\HttpClient\Options::HANDLER_RESPONSE => static function (Psr\Http\Message\RequestInterface $request, Psr\Http\Message\ResponseInterface $response) { return getimagesizefromstring($response->getBody()->getContents()); }, ], $concurrency = 2); print_r($result); // Output: // //Array //( // [png] => Array // ( // [0] => 100 // [1] => 100 // [2] => 3 // [3] => width="100" height="100" // [bits] => 8 // [mime] => image/png // ) // // [webp] => Array // ( // [0] => 274 // [1] => 367 // [2] => 18 // [3] => width="274" height="367" // [bits] => 8 // [mime] => image/webp // ) // // [jpeg] => Array // ( // [0] => 239 // [1] => 178 // [2] => 2 // [3] => width="239" height="178" // [bits] => 8 // [channels] => 3 // [mime] => image/jpeg // ) //)
变更日志
变更记录在 发布页面 上。
许可证
此存档中的文件是在 MIT许可证
下发布的。
您可以在 LICENSE
文件中找到此许可证的副本。