chillerlan/php-httpinterface
Composer 安装命令:
composer require chillerlan/php-httpinterface
包简介
A PSR-7/17/18 http message/client implementation
README 文档
README
A PSR-7/PSR-17/PSR-18 HTTP message/client implementation.
Documentation
An API documentation created with phpDocumentor can be found at https://chillerlan.github.io/php-httpinterface/ (WIP).
Requirements
- PHP 8.4+
ext-curl- from dependencies:
Installation with composer
Terminal
composer require chillerlan/php-httpinterface
composer.json
{
"require": {
"php": "^8.4",
"chillerlan/php-httpinterface": "dev-main#<commit_hash>"
}
}
Note: replace dev-main with a version constraint, e.g. ^7.0 - see releases for valid versions.
Profit!
Quickstart
The HTTP clients CurlClient and StreamClient are invoked with a ResponseFactoryInterface instance
as the first parameter, followed by optional HTTPOptions and PSR-3 LoggerInterface instances.
You can then send a request via the implemented PSR-18 method ClientInterface::sendRequest(),
using a PSR-7 RequestInterface and expect a PSR-7 ResponseInterface.
CurlClient, StreamClient
$options = new HTTPOptions; $options->user_agent = 'my cool user agent 1.0'; $options->ca_info = '/path/to/cacert.pem'; $options->dns_over_https = 'https://cloudflare-dns.com/dns-query'; $options->curl_check_OCSP = true; $options->curl_check_doh_OCSP = true; $httpClient = new CurlClient($responseFactory, $options, $logger); $request = $requestFactory->createRequest('GET', 'https://www.example.com?foo=bar'); $httpClient->sendRequest($request);
CurlMultiClient
The CurlMultiClient client implements asynchronous multi requests ("rolling-curl").
It needs a MultiResponseHandlerInterface that parses the incoming responses, the callback may return a failed request to the stack:
$handler = new class () implements MultiResponseHandlerInterface{ public function handleResponse( ResponseInterface $response, // the incoming response RequestInterface $request, // the corresponding request int $id, // the request id array|null $curl_info, // the curl_getinfo() result for this request ):RequestInterface|null{ if($response->getStatusCode() !== 200){ // return the failed request back to the stack return $request; } try{ $body = $response->getBody(); // the response body is empty for some reason, we pretend that's fine and exit if($body->getSize() === 0){ return null; } // parse the response body, store the result etc. $data = $body->getContents(); // save data to file, database or whatever... // ... } catch(Throwable){ // something went wrong, return the request to the stack for another try return $request; } // everything ok, nothing to return return null; } };
You can then invoke the multi request client - the MultiResponseHandlerInterface and ResponseFactoryInterface are mandatory,
HTTPOptions and LoggerInterface are optional:
$options = new HTTPOptions; $options->ca_info = '/path/to/cacert.pem'; $options->user_agent = 'my cool user agent 1.0'; $options->sleep = 750000; // microseconds, see usleep() $options->window_size = 5; $options->retries = 1; $multiClient = new CurlMultiClient($handler, $responseFactory, $options, $logger); // create and add the requests foreach(['..', '...', '....'] as $item){ $multiClient->addRequest($factory->createRequest('GET', $endpoint.'/'.$item)); } // process the queue $multiClient->process();
Auto generated API documentation
The API documentation can be auto generated with phpDocumentor. There is an online version available via the gh-pages branch that is automatically deployed on each push to main.
Locally created docs will appear in the directory .build/phpdocs/. If you'd like to create local docs, please follow these steps:
- download phpDocumentor v3+ as .phar archive
- run it in the repository root directory:
- on Windows
c:\path\to\php.exe c:\path\to\phpDocumentor.phar --config=phpdoc.xml - on Linux just
php /path/to/phpDocumentor.phar --config=phpdoc.xml
- on Windows
- open index.html in a browser
- profit!
Disclaimer
Use at your own risk!
chillerlan/php-httpinterface 适用场景与选型建议
chillerlan/php-httpinterface 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.1k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2018 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「client」 「response」 「message」 「request」 「factory」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chillerlan/php-httpinterface 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chillerlan/php-httpinterface 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chillerlan/php-httpinterface 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
HTTP request logger middleware for Laravel
Mock PSR-18 HTTP client
Class to generate a standard structure for api json responses
Asynchronous MQTT client built on React
http客户端
统计信息
- 总下载量: 18.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 28
- 依赖项目数: 5
- 推荐数: 5
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-23