voku/httpful
Composer 安装命令:
composer require voku/httpful
包简介
A Readable, Chainable, REST friendly, PHP HTTP Client
README 文档
README
📯 Httpful
Forked some years ago from nategood/httpful + added support for parallel request and implemented many PSR Interfaces: A Chainable, REST Friendly Wrapper for cURL with many "PSR-HTTP" implemented interfaces.
Features
- Readable HTTP Method Support (GET, PUT, POST, DELETE, HEAD, PATCH and OPTIONS)
- Custom Headers
- Automatic "Smart" Parsing
- Automatic Payload Serialization
- Basic Auth
- Bearer Token Auth
- Client Side Certificate Auth (SSL)
- Retry Configuration (count, delay, max time, all-errors, connection-refused)
- Advanced TLS Configuration (CA bundle / path, pinned public key, TLS version)
- Cookie Persistence (cookie file / cookie jar)
- Modern HTTP Version Helpers (HTTP/2 prior knowledge, HTTP/3, HTTP/3 only)
- Alt-Svc / HSTS Cache Helpers
- Proxy / Routing Helpers (no-proxy, proxy tunnel, resolve, connect-to)
- Request "Download"
- Async Request Helper (
Request::sendAsync()) - Request "Templates"
- Parallel Request (via curl_multi)
- Transfer Metadata Helpers
- Curl-Style Alias Helpers (
downloadTo(),authenticateWith*(),useHttp*(), ...) - PSR-3: Logger Interface
- PSR-7: HTTP Message Interface
- PSR-17: HTTP Factory Interface
- PSR-18: HTTP Client Interface
Examples
<?php // Make a request to the GitHub API. $uri = 'https://api.github.com/users/voku'; $response = \Httpful\Client::get($uri, null, \Httpful\Mime::JSON); echo $response->getBody()->name . ' joined GitHub on ' . date('M jS Y', strtotime($response->getBody()->created_at)) . "\n";
<?php // Make a request to the GitHub API with a custom // header of "X-Foo-Header: Just as a demo". $uri = 'https://api.github.com/users/voku'; $response = \Httpful\Client::get_request($uri)->withAddedHeader('X-Foo-Header', 'Just as a demo') ->expectsJson() ->send(); $result = $response->getRawBody(); echo $result['name'] . ' joined GitHub on ' . \date('M jS Y', \strtotime($result['created_at'])) . "\n";
<?php // BasicAuth example with MultiCurl for async requests. /** @var \Httpful\Response[] $results */ $results = []; $multi = new \Httpful\ClientMulti( static function (\Httpful\Response $response, \Httpful\Request $request) use (&$results) { $results[] = $response; } ); $request = (new \Httpful\Request(\Httpful\Http::GET)) ->withUriFromString('https://postman-echo.com/basic-auth') ->withBasicAuth('postman', 'password'); $multi->add_request($request); // $multi->add_request(...); // add more calls here $multi->start(); // DEBUG //print_r($results);
<?php $promise = \Httpful\Request::get('https://api.example.com/items') ->authenticateWithBearerToken('secret-token') ->sendAsync(); $response = $promise->wait(); echo $response->getCode() . "\n";
<?php $response = \Httpful\Request::get('https://api.example.com/items') ->withBearerToken('secret-token') ->withRetry(3) ->withRetryDelay(1) ->withRetryMaxTime(10) ->withCookieJar('/tmp/httpful.cookies') ->withCaBundle('/etc/ssl/certs/ca-bundle.crt') ->withHttp2PriorKnowledge() ->send(); echo $response->getEffectiveUrl() . "\n"; echo $response->getTransferHttpVersion() . "\n"; echo $response->getTotalTime() . "\n";
Installation
composer require voku/httpful
Requires PHP 8.0+. Compared with 3.1.0, the only intended breaking change is the PHP 8.0 minimum; the new request and response helpers are additive.
Handlers
We can override the default parser configuration options be registering a parser with different configuration options for a particular mime type
Example: setting a namespace for the XMLHandler parser
$conf = ['namespace' => 'http://example.com']; \Httpful\Setup::registerMimeHandler(\Httpful\Mime::XML, new \Httpful\Handlers\XmlMimeHandler($conf));
Handlers are simple classes that are used to parse response bodies and serialize request payloads. All Handlers must implement the MimeHandlerInterface interface and implement two methods: serialize($payload) and parse($response). Let's build a very basic Handler to register for the text/csv mime type.
<?php class SimpleCsvMimeHandler extends \Httpful\Handlers\DefaultMimeHandler { /** * Takes a response body, and turns it into * a two dimensional array. * * @param string $body * * @return array */ public function parse($body) { return \str_getcsv($body); } /** * Takes a two dimensional array and turns it * into a serialized string to include as the * body of a request * * @param mixed $payload * * @return string */ public function serialize($payload) { // init $serialized = ''; foreach ($payload as $line) { $serialized .= '"' . \implode('","', $line) . '"' . "\n"; } return $serialized; } } \Httpful\Setup::registerMimeHandler(\Httpful\Mime::CSV, new SimpleCsvMimeHandler());
Finally, you must register this handler for a particular mime type.
\Httpful\Setup::register(Mime::CSV, new SimpleCsvHandler());
After this registering the handler in your source code, by default, any responses with a mime type of text/csv should be parsed by this handler.
voku/httpful 适用场景与选型建议
voku/httpful 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 185.83k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2016 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curl」 「http」 「rest」 「api」 「restful」 「requests」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 voku/httpful 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 voku/httpful 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 voku/httpful 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
cURL and cURL based Soap-Client for PHP
repository php library
Simple cURL wrapper
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
A fluent PHP CURL wrapper
统计信息
- 总下载量: 185.83k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-29