Download the PHP package kode/http-client without Composer
On this page you can find all versions of the php package kode/http-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package http-client
kode/http-client
一个现代化、高性能的 PHP HTTP 客户端,支持多运行时环境(FPM、CLI、Swoole、Swow、Fiber)。
特性
- ✅ 多运行时支持 - 自动检测运行环境,支持 FPM、CLI、Swoole、Swow、Fiber
- ✅ 自动驱动切换 - 根据环境自动选择最优驱动
- ✅ PSR-7/PSR-18 兼容 - 完全遵循 PSR 标准
- ✅ 上下文管理 - 使用
kode/context进行请求上下文传递 - ✅ 中间件机制 - 内置重试、超时、日志、认证、限流、缓存等中间件
- ✅ PHP 8.3+ 支持 - 兼容 PHP 8.4 / 8.5 新特性
- ✅ Fiber 支持 - 原生 Fiber 驱动,支持 kode/fibers 包集成
- ✅ 类型安全 - 完整的类型声明和静态分析支持
安装
快速开始
基本使用
使用 Fiber 驱动
使用配置选项
驱动支持
| 运行环境 | 推荐驱动 | 说明 |
|---|---|---|
| Fiber 环境 | FiberDriver |
PHP 8.3+ 原生 Fiber 支持,自动协程切换 |
| Swoole 协程 | SwooleDriver |
高性能、原生协程支持 |
| Amp 环境 | AmpDriver |
基于事件循环的异步实现 |
| 默认环境 | CurlDriver |
基于 curl 扩展的同步实现 |
驱动选择优先级
自动模式下,驱动选择优先级为:
- SwooleDriver - 如果在 Swoole 协程环境中
- FiberDriver - 如果 PHP 8.3+ 且 curl 扩展可用
- AmpDriver - 如果 Amp HTTP 客户端可用
- CurlDriver - 默认回退
手动选择驱动
中间件
认证中间件
支持 Bearer Token 和 API Key 认证:
限流中间件
使用令牌桶算法实现请求频率限制:
缓存中间件
自动缓存 GET 请求响应:
重试中间件
支持指数退避策略的自动重试:
超时中间件
为请求设置超时时间:
日志中间件
记录请求和响应信息:
链路追踪中间件
基于 kode/context 3.x 的 toHeaders() / fromHeaders(),自动把当前链路上下文
(W3C traceparent / tracestate 以及 X-Context-* 私有头)注入出站请求,
打通跨服务全链路追踪:
开启链路追踪需在请求前调用 Kode\Context\Context::startTrace()(或复用已有链路);
未开启时仍会透传已存在的 X-Context-* 头(如请求 ID、关联 ID),但不会注入 W3C 头。
可通过工厂一键启用:
上下文管理
使用 kode/context 进行上下文传递:
与 kode/fibers 集成
本包支持与 kode/fibers 包无缝集成:
异常处理
项目结构
示例
example/ 下提供了可直接运行的示例:
| 文件 | 说明 | 联网 |
|---|---|---|
example/quickstart.php |
基础用法:GET/POST、JSON/表单、base_uri、单次超时 | 需要 |
example/middleware.php |
中间件装配、自定义中间件、各中间件运行时状态 | 不需要 |
example/concurrent.php |
sendConcurrent() / pool() 的「全部落定」语义 |
不需要 |
example/tracing.php |
链路追踪头注入与安全降级 | 不需要 |
不联网的示例使用桩驱动(实现 DriverInterface),结果确定,也是单元测试的惯用写法。
测试
要求
- PHP >= 8.3
- ext-curl(CurlDriver 和 FiberDriver 需要)
- kode/context ^3.1
- psr/http-message ^2.0
- psr/http-client ^1.0
可选依赖
- ext-swoole - Swoole 协程支持
- ext-swow - Swow 协程支持
- amphp/http-client - Amp 异步 HTTP 客户端
- kode/fibers - Fiber 协程池和调度器
- guzzlehttp/psr7 - PSR-7 消息实现
许可证
MIT
作者
Kode Team [email protected]
All versions of http-client with dependencies
ext-json Version *
kode/context Version ^3.1
psr/http-client Version ^1.0
psr/http-factory Version ^1.0
psr/http-message Version ^2.0