riftfox/php-http-client
最新稳定版本:v0.1.0
Composer 安装命令:
composer require riftfox/php-http-client
包简介
A lightweight PSR-18 HTTP client wrapper allowing custom response transformation via result factories.
README 文档
README
A lightweight PSR-18 HTTP client wrapper allowing custom response transformation via result factories.
Requirements
- PHP >= 7.4
- A PSR-18 HTTP Client implementation (e.g., Guzzle, Symfony HttpClient)
- PSR-7 HTTP Message implementation
Installation
composer require riftfox/php-http-client
Usage
This library separates the HTTP transport (PSR-18) from the response processing logic (ResultFactory).
1. Create a Result and a Factory
Implement ResultInterface for your data object and ResultFactoryInterface to transform the PSR-7 response into that object.
namespace App\Http; use Riftfox\HttpClient\ResultInterface; use Riftfox\HttpClient\ResultFactoryInterface; use Psr\Http\Message\ResponseInterface; class UserResult implements ResultInterface { public array $data; public function __construct(array $data) { $this->data = $data; } } class UserResultFactory implements ResultFactoryInterface { public function create(ResponseInterface $response): ResultInterface { $json = (string) $response->getBody(); $data = json_decode($json, true); return new UserResult($data ?? []); } }
2. Send a Request
use Riftfox\HttpClient\HttpClient; use GuzzleHttp\Client as GuzzleClient; // Example PSR-18 Client use GuzzleHttp\Psr7\Request; // 1. Initialize the PSR-18 client and your wrapper $psrClient = new GuzzleClient(); $client = new HttpClient($psrClient); // 2. Prepare the request and the factory $request = new Request('GET', 'https://api.example.com/users/1'); $factory = new UserResultFactory(); // 3. Send request and get the transformed result directly /** @var UserResult $result */ $result = $client->send($request, $factory); print_r($result->data);
Testing
Run the test suite with Codeception:
vendor/bin/codecept run unit
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-26