{"id":20282292,"url":"https://github.com/ne-lexa/http-client-simple-cache","last_synced_at":"2025-04-11T07:59:58.496Z","repository":{"id":57024777,"uuid":"198619966","full_name":"Ne-Lexa/http-client-simple-cache","owner":"Ne-Lexa","description":"Guzzle-based HTTP Client with the ability to customize caching of the processed HTTP request results (not based on HTTP headers).","archived":false,"fork":false,"pushed_at":"2021-02-25T20:06:13.000Z","size":37,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-09T15:50:22.033Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ne-Lexa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-24T11:14:06.000Z","updated_at":"2021-02-25T20:06:15.000Z","dependencies_parsed_at":"2022-08-23T13:50:32.335Z","dependency_job_id":null,"html_url":"https://github.com/Ne-Lexa/http-client-simple-cache","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ne-Lexa%2Fhttp-client-simple-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ne-Lexa%2Fhttp-client-simple-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ne-Lexa%2Fhttp-client-simple-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ne-Lexa%2Fhttp-client-simple-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ne-Lexa","download_url":"https://codeload.github.com/Ne-Lexa/http-client-simple-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224632817,"owners_count":17343925,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T14:08:50.110Z","updated_at":"2024-11-14T14:08:50.918Z","avatar_url":"https://github.com/Ne-Lexa.png","language":"PHP","readme":"# nelexa/http-client-simple-cache\n\nGuzzle-based HTTP Client with the ability to customize caching of the processed HTTP request results (not based on HTTP headers).\n\n[![Packagist Version](https://img.shields.io/packagist/v/nelexa/http-client-simple-cache.svg?style=popout)](https://packagist.org/packages/nelexa/http-client-simple-cache)\n![PHP from Packagist](https://img.shields.io/packagist/php-v/nelexa/http-client-simple-cache.svg?style=popout\u0026color=yellowgreen)\n[![License](https://img.shields.io/packagist/l/nelexa/http-client-simple-cache.svg?style=popout\u0026color=01f176)](https://packagist.org/packages/nelexa/http-client-simple-cache)\n\n[![Build Status](https://github.com/Ne-Lexa/http-client-simple-cache/workflows/build/badge.svg)](https://github.com/Ne-Lexa/http-client-simple-cache/actions)\n![Scrutinizer build](https://img.shields.io/scrutinizer/build/g/Ne-Lexa/http-client-simple-cache/master.svg?label=Scrutinizer\u0026style=popout)\n![Scrutinizer code quality](https://img.shields.io/scrutinizer/quality/g/Ne-Lexa/http-client-simple-cache/master.svg?style=popout)\n![Scrutinizer coverage](https://img.shields.io/scrutinizer/coverage/g/Ne-Lexa/http-client-simple-cache/master.svg?style=popout)\n\n\n## Documentation\nGuzzle docs: http://docs.guzzlephp.org/en/stable/\n\n### Init\n```php\n\u003c?php\n\n$client = new \\Nelexa\\HttpClient\\HttpClient();\n```\nSet default options:\n```php\n\u003c?php\n\n$client = new \\Nelexa\\HttpClient\\HttpClient([\n    \\Nelexa\\HttpClient\\Options::HEADERS =\u003e [\n        'User-Agent' =\u003e 'TestHttpClient/1.0',\n    ],\n]);\n```\n\n## Processing an HTTP request and getting the result\n```php\n\u003c?php\n\n$client = new \\Nelexa\\HttpClient\\HttpClient();\n$result = $client-\u003eget($url, [\n    \\Nelexa\\HttpClient\\Options::HANDLER_RESPONSE =\u003e $callable\n]);\n```\nCallable signature:\n```php\nfunction(\\Psr\\Http\\Message\\RequestInterface $request, \\Psr\\Http\\Message\\ResponseInterface $response){\n    return ...;\n}\n```\nExample:\n```php\n\u003c?php\n\n$client = new \\Nelexa\\HttpClient\\HttpClient();\n\n// use \\Closure handler\n\n$base64Contents = $client-\u003eget($url, [\n    \\Nelexa\\HttpClient\\Options::HANDLER_RESPONSE =\u003e static function (Psr\\Http\\Message\\RequestInterface $request, Psr\\Http\\Message\\ResponseInterface $response) {\n        return base64_encode($response-\u003egetBody()-\u003egetContents());\n    },\n]);\n\n// or use class handler\n\n$base64Contents = $client-\u003eget($url, [\n    \\Nelexa\\HttpClient\\Options::HANDLER_RESPONSE =\u003e new class() implements \\Nelexa\\HttpClient\\ResponseHandlerInterface {\n    \n        public function __invoke(Psr\\Http\\Message\\RequestInterface $request, Psr\\Http\\Message\\ResponseInterface $response)\n        {\n            return base64_encode($response-\u003egetBody()-\u003egetContents());\n        }\n    },\n]);\n```\n\n## Use cache results\nInstall the implementation of the simple cache PSR-16. \n\nFull list of packages https://packagist.org/providers/psr/simple-cache-implementation\n\nExample install:\n```bash\ncomposer require symfony/cache\n```\n\nAdd option `\\Nelexa\\HttpClient\\Options::CACHE_TTL` with `\\DateInterval` value.\n\nExample:\n```php\n\u003c?php\n\nclass Api\n{\n    /** @var \\Nelexa\\HttpClient\\HttpClient */\n    private $httpClient;\n\n    public function __construct(Psr\\SimpleCache\\CacheInterface $cache)\n    {\n        $this-\u003ehttpClient = new \\Nelexa\\HttpClient\\HttpClient([], $cache);\n    }\n\n    /**\n     * Fetch uuid.\n     *\n     * @return string\n     */\n    public function fetchUUID(): string\n    {\n        return $this-\u003ehttpClient-\u003erequest('GET', 'https://httpbin.org/uuid', [\n            \n            \\Nelexa\\HttpClient\\Options::CACHE_TTL =\u003e \\DateInterval::createFromDateString('1 min'), // required TTL\n            \n            \\Nelexa\\HttpClient\\Options::HANDLER_RESPONSE =\u003e static function (Psr\\Http\\Message\\RequestInterface $request, Psr\\Http\\Message\\ResponseInterface $response) {\n                $contents = $response-\u003egetBody()-\u003egetContents();\n                $json = \\GuzzleHttp\\json_decode($contents, true);\n\n                return $json['uuid'];\n            },\n        ]);\n    }\n}\n\n$cache = new \\Symfony\\Component\\Cache\\Psr16Cache(\n    new \\Symfony\\Component\\Cache\\Adapter\\RedisAdapter(\n        \\Symfony\\Component\\Cache\\Adapter\\RedisAdapter::createConnection('redis://localhost')\n    )\n);\n\n$api = new Api($cache);\n$UUID = $api-\u003efetchUUID();\n\n\\PHPUnit\\Framework\\Assert::assertSame($api-\u003efetchUUID(), $UUID);\n\nvar_dump($UUID); // string(36) \"a72b27c2-7e69-4bc8-8d5b-ccb0e496a7bf\"\n```\n\n## Async Request Pool Handler\n```php\n\u003c?php\n\n$urls = [\n    'jpeg' =\u003e 'https://httpbin.org/image/jpeg',\n    'png' =\u003e 'https://httpbin.org/image/png',\n    'webp' =\u003e 'https://httpbin.org/image/webp',\n];\n\n$client = new \\Nelexa\\HttpClient\\HttpClient();\n$result = $client-\u003erequestAsyncPool('GET', $urls, [\n    \\Nelexa\\HttpClient\\Options::HANDLER_RESPONSE =\u003e static function (Psr\\Http\\Message\\RequestInterface $request, Psr\\Http\\Message\\ResponseInterface $response) {\n        return getimagesizefromstring($response-\u003egetBody()-\u003egetContents());\n    },\n], $concurrency = 2);\n\nprint_r($result);\n\n// Output:\n//\n//Array\n//(\n//    [png] =\u003e Array\n//    (\n//            [0] =\u003e 100\n//            [1] =\u003e 100\n//            [2] =\u003e 3\n//            [3] =\u003e width=\"100\" height=\"100\"\n//            [bits] =\u003e 8\n//            [mime] =\u003e image/png\n//    )\n//\n//    [webp] =\u003e Array\n//    (\n//            [0] =\u003e 274\n//            [1] =\u003e 367\n//            [2] =\u003e 18\n//            [3] =\u003e width=\"274\" height=\"367\"\n//            [bits] =\u003e 8\n//            [mime] =\u003e image/webp\n//    )\n//\n//    [jpeg] =\u003e Array\n//    (\n//            [0] =\u003e 239\n//            [1] =\u003e 178\n//            [2] =\u003e 2\n//            [3] =\u003e width=\"239\" height=\"178\"\n//            [bits] =\u003e 8\n//            [channels] =\u003e 3\n//            [mime] =\u003e image/jpeg\n//    )\n//)\n```\n\n# Changelog\n\nChanges are documented in the [releases page](https://github.com/Ne-Lexa/http-client-simple-cache/releases).\n\n# License\n\nThe files in this archive are released under the `MIT License`.\n \nYou can find a copy of this license in `LICENSE` file.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fne-lexa%2Fhttp-client-simple-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fne-lexa%2Fhttp-client-simple-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fne-lexa%2Fhttp-client-simple-cache/lists"}