定制 navari/http-client-simple-cache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

navari/http-client-simple-cache

Composer 安装命令:

composer require navari/http-client-simple-cache

包简介

Guzzle-based HTTP Client with the ability to customize caching of the processed HTTP request results (not based on HTTP headers).

README 文档

README

Guzzle-based HTTP Client with the ability to customize caching of the processed HTTP request results (not based on HTTP headers).

Packagist Version PHP from Packagist License

Travis Build Status Scrutinizer build Scrutinizer code quality Scrutinizer coverage

Documentation

Guzzle docs: http://docs.guzzlephp.org/en/stable/

Init

<?php

$client = new \Nelexa\HttpClient\HttpClient();

Set default options:

<?php

$client = new \Nelexa\HttpClient\HttpClient([
    \Nelexa\HttpClient\Options::HEADERS => [
        'User-Agent' => 'TestHttpClient/1.0',
    ],
]);

Processing an HTTP request and getting the result

<?php

$client = new \Nelexa\HttpClient\HttpClient();
$result = $client->get($url, [
    \Nelexa\HttpClient\Options::HANDLER_RESPONSE => $callable
]);

Callable signature:

function(\Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response){
    return ...;
}

Example:

<?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());
        }
    },
]);

Use cache results

Install the implementation of the simple cache PSR-16.

Full list of packages https://packagist.org/providers/psr/simple-cache-implementation

Example install:

composer require symfony/cache

Add option \Nelexa\HttpClient\Options::CACHE_TTL with \DateInterval value.

Example:

<?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"

Async Request Pool Handler

<?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
//    )
//)

Changelog

Changes are documented in the releases page.

License

The files in this archive are released under the MIT License.

You can find a copy of this license in LICENSE file.

navari/http-client-simple-cache 适用场景与选型建议

navari/http-client-simple-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.31k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 navari/http-client-simple-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 navari/http-client-simple-cache 我们能提供哪些服务?
定制开发 / 二次开发

基于 navari/http-client-simple-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-25