webservco/http-client
Composer 安装命令:
composer require webservco/http-client
包简介
A PHP component/library.
关键字:
README 文档
README
A minimalist PHP HTTP Client implementation using cURL.
Test project: webservco/http-client-test
Installation
composer.json:
{
"require": {
"webservco/http-client": "^0"
}
}
Usage
PSR-18 HTTP client implementation
- Regular usage (send a request, receive a response);
- Fully PSR compliant (can be used as a drop-in replacement);
// PSR-18: \Psr\Http\Client\ClientInterface $httpClient = new HttpClient( // \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface $curlService, ); $response = $httpClient->sendRequest( // PSR-7: \Psr\Http\Message\RequestInterface $request, );
Custom multi processing HTTP client implementation
- Use to send multiple requests in parallel via cURL multi handle;
- Custom implementation, though input and output are PSR compliant;
// \WebServCo\Http\Client\Contract\Service\cURL\CurlMultiServiceInterface $curlMultiService = new CurlMultiService( // \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface $curlService ); /** @var array<int,\Psr\Http\Message\RequestInterface> $requests */ $requests = [ // PSR-7: \Psr\Http\Message\RequestInterface 1 => $request1, // PSR-7: \Psr\Http\Message\RequestInterface 2 => $request2, // etc ]; // Keep a list of handles to be able to link them to each id. key: id, value: handle identifier. /** @var array<int,string> $curlHandleIdentifiers */ $curlHandleIdentifiers = []; foreach ($requests as $request) { // Create handle and add it's identifier to the list. $handleIdentifier = $curlMultiService->createHandle($request); $curlHandleIdentifiers[$releaseId] = $handleIdentifier; } // Add more requests if needed // Create handle and add it's identifier to the list. $handleIdentifier = $curlMultiService->createHandle($request); $curlHandleIdentifiers[$releaseId] = $handleIdentifier; // When ready, execute all requests in parallel $curlMultiService->executeSessions(); // Retrieve responses // Use case: consumer keeps track of requests added and needs to be able to identify corresponding responses. foreach ($curlHandleIdentifiers as $releaseId => $handleIdentifier) { $response = $curlMultiService->getResponse($handleIdentifier); // Do something with the response } // Use case: consumer just needs all responses, no need to link them to the requests. foreach ($curlMultiService->iterateResponse() as $response) { // Do something with the response } // Cleanup. After this the service can be re-used, going through all the steps. $curlMultiService->cleanup();
Initialization
CurlServiceInterface
Main service class, needed for any chosen usage.
Dependencies: any PSR-17 implementation can be used.
// \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface $curlService = new CurlService( // \WebServCo\Http\Client\DataTransfer\CurlServiceConfiguration new CurlServiceConfiguration( // enableDebugMode true ), // \WebServCo\Log\Contract\LoggerFactoryInterface new ContextFileLoggerFactory(sprintf('%svar/log', $projectPath)), // PSR-17: \Psr\Http\Message\ResponseFactoryInterface $responseFactory, // PSR-17: \Psr\Http\Message\StreamFactoryInterface $streamFactory, );
An example factory using webservco/http implementation
<?php declare(strict_types=1); namespace Project\Http\Client\Factory; use WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface; use WebServCo\Http\Client\DataTransfer\CurlServiceConfiguration; use WebServCo\Http\Client\Service\cURL\CurlService; use WebServCo\Http\Factory\Message\Response\ResponseFactory; use WebServCo\Http\Factory\Message\Stream\StreamFactory; use WebServCo\Http\Service\Message\Response\StatusCodeService; use WebServCo\Log\Contract\LoggerFactoryInterface; final class CurlServiceFactory { public function __construct(private LoggerFactoryInterface $loggerFactory) { } public function createCurlService(int $timeout): CurlServiceInterface { $streamFactory = new StreamFactory(); return new CurlService( new CurlServiceConfiguration(true, $timeout), $this->loggerFactory, new ResponseFactory(new StatusCodeService(), $streamFactory), $streamFactory, ); } }
$loggerFactory = new ContextFileLoggerFactory(sprintf('%svar/log', $projectPath)); new CurlServiceFactory($loggerFactory);
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
webservco/http-client 适用场景与选型建议
webservco/http-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 322 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「component」 「webservco」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webservco/http-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webservco/http-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webservco/http-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
FSi DataSource Component
Custom, opinionated coding standards based on PSR12, SlevomatCodingStandard, and PHPCompatibility.
Priveate for SkeekS CMS
A PHP RESTful API Framework
A PHP component/library.
A PHP component/library.
统计信息
- 总下载量: 322
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-09