innmind/http-transport
Composer 安装命令:
composer require innmind/http-transport
包简介
Library to fulfill http resquest
README 文档
README
This library allows you to send http request.
Important
to use this library correctly you must use vimeo/psalm.
Installation
composer require innmind/http-transport
Usage
Send a request:
use Innmind\HttpTransport\Transport; use Innmind\Time\Clock; use Innmind\Http\Request; $fulfill = Transport::curl(Clock::live()); $either = $fulfill( Request::of(/* initialize your request */), );
2xx responses will be on the right side of $either, all errors and other kinds of responses will be on the left side.
Important
you must call match to the returned Either otherwise the request will not be sent, but you can still call other methods on the Either before calling match.
Concurrency
By default there is no limit of concurrency for the Curl transport. But if you call many requests before unwrapping the results you may want to configure the max concurrency like below.
use Innmind\HttpTransport\Transport; use Innmind\Http\{ Request, Response, Method, ProtocolVersion, }; use Innmind\Url\Url; use Innmind\Immutable\Sequence; $fulfill = Transport::curl(Clock::live())->map( static fn($config) => $config->limitConcurrencyTo(5), ); $responses = Sequence::of( 'https://github.com/user/repo-a', 'https://github.com/user/repo-b', 'https://github.com/user/repo-c', // etc... ) ->map(static fn($url) => Request::of( Url::of($url), Method::get, ProtocolVersion::v20, )) ->map($fulfill) ->flatMap(static fn($either) => $either->match( static fn($success) => Sequence::of($success->response()), static fn() => Sequence::of(), // discard errors )) ->toList(); $responses; // list<Response>
Let's say you have 100 urls to fetch, there will never be more than 5 requests being done in parallel.
Log the request
You can easily log all your requests like so:
use Innmind\HttpTransport\Transport; use Psr\Log\LoggerInterface; $fulfill = Transport::logger(/* an instance of Transport */, /* an instance of LoggerInterface */) $fulfill(/* your request */);
Here a message is logged before the request is sent and another one once it's sent.
Exponential Backoff
Sometimes when calling an external API it may not be available due to heavy load, in such case you could retry the http call after a certain amount of time leaving time for the API to recover. You can apply this pattern like so:
use Innmind\HttpTransport\Transport; use Innmind\Time\Halt; $fulfill = Transport::exponentialBackoff( /* an instance of Transport */, Halt::new(), ); $fulfill(/* your request */);
By default it will retry 5 times the request if the server is unavailable, following the given periods (in milliseconds) between each call: 100, 271, 738, 2008 and 5459.
Circuit breaker
When a call to a certain domain fails you may want to all further calls to that domain to fail immediately as you know it means the host is down. Such pattern is called a circuit breaker.
use Innmind\HttpTransport\Transport; use Innmind\Time\{ Clock, Period, }; $fulfill = Transport::circuitBreaker( /* an instance of Transport */, Clock::live(), Period::minute(10), ); $fulfill(/* your request */);
This code will open the circuit for a given domain for 10 minutes in case a call results in a server error, after this delay the transport will let new request through as if nothing happened.
Follow redirections
By default the transports do not follow redirections to give you full control on what to do. But you can wrap your transport with FollowRedirections like this:
use Innmind\HttpTransport\Transport; $fulfill = Transport::followRedirections(/* an instance of Transport */); $fulfill(/* your request */);
To avoid infinite loops it will follow up to 5 consecutive redirections.
Important
as defined in the rfc, requests with methods other than GET and HEAD that results in redirection with the codes 301, 302, 307 and 308 will NOT be redirected. It will be up to you to implement the redirection as you need to make sure such redirection is safe.
innmind/http-transport 适用场景与选型建议
innmind/http-transport 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 104.46k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 11 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 innmind/http-transport 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 innmind/http-transport 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 innmind/http-transport 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
http客户端
Easily add Excepct-CT header to your project
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 104.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-11-15