Download the PHP package yunadmin/http-client without Composer
On this page you can find all versions of the php package yunadmin/http-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yunadmin/http-client
More information about yunadmin/http-client
Files in yunadmin/http-client
Package http-client
Short Description 零依赖轻量级 HTTP 客户端类库,支持 cURL/Stream 双引擎、中间件、重定向、重试、JSON/FORM 表单、文件上传。
License MIT
Homepage https://github.com/yunadmin-cn/http-client
Informations about the package http-client
yunadmin/http-client
仓库:https://github.com/yunadmin-cn/http-client
安装:composer require yunadmin/http-client
零依赖、轻量级的 PHP HTTP 客户端。仅依赖 PHP ≥ 8.0 的原生扩展(curl / json),无任何 Composer 第三方包。
特性
| 能力 | 说明 |
|---|---|
| 双引擎 | cURL (CurlHandler) / Socket Stream (StreamHandler) 自动选择 |
| 异步并发 | CurlMultiHandler 基于 cURL Multi;Future、requestAsync()、pool()(并发度控制) |
| 流式上传 | 按 8KB chunk 读取 body,未知长度自动走 Transfer-Encoding: chunked,大文件友好 |
| 中间件系统 | HandlerStack 管道;内置重定向、HTTP 错误抛出、指数退避重试、历史记录 |
| Cookie 会话 | CookieJar + 标准 Set-Cookie 解析(支持引号内嵌分号) |
| 可测试 | MockHandler 响应队列 + HistoryMiddleware 历史记录,不发真实请求即可测业务 |
| 常用选项对齐 | decode_content、force_ip_resolve、timeout、connect_timeout、verify、cert/ssl_key、proxy、auth、on_stats、sink、json/form_params/multipart/body、query、base_uri、curl |
| PSR-7 风格对象 | Request / Response / Uri / Stream / StreamInterface |
要求:php ^8.0、ext-curl、ext-json(已在 composer.json 声明)。
安装
或手动引入:只需注册 PSR-4 前缀 Yunadmin\HttpClient\ 指向 src/,并 require 'src/functions.php'。
1. 快速开始
2. HTTP 方法
便捷方法:get / head / post / put / patch / delete / options,每个都接受 (uri, options)。
对应异步版:getAsync / postAsync / putAsync / patchAsync / deleteAsync / requestAsync,返回 Future(见第 6 节)。
3. 请求体:JSON / 表单 / 文件上传
3.1 JSON(json)
3.2 URL 编码表单(form_params)
3.3 multipart 表单 & 文件上传(multipart)
说明:
contents支持 string / resource / StreamInterface 三种。filename省略时,若contents是文件资源会自动用stream_get_meta_data()['uri'],否则不生成filename=。- 会自动生成 boundary,
Content-Type: multipart/form-data; boundary=...。 - 大文件按 8KB chunk 读取,不会一次性进内存。
3.4 原始 body(body)
getSize() != null→ 自动设置Content-LengthgetSize() == null→ 自动使用Transfer-Encoding: chunked
4. URI 处理
4.1 base_uri + 相对 URI
4.2 query 选项
4.3 Uri 类(PSR-7 风格)
5. 认证、代理、超时、TLS
6. 异步 & 并发(cURL Multi)
6.1 getAsync / postAsync / requestAsync → 返回 Future
6.2 Future 链式回调
链式回调会在
wait()时按顺序执行(轻量实现,不依赖 Promise/A+ 库)。
6.3 pool() — 大列表 + 并发度控制
$requests 每一项支持:
['GET', $uri]['POST', $uri, ['json' => [...]]]- 直接传
RequestInterface实例
7. 重定向、HTTP 错误、重试
7.1 重定向(allow_redirects)
内置 RedirectMiddleware;超过 max 抛 TooManyRedirectsException。
7.2 HTTP 错误(http_errors)
默认 4xx / 5xx 会抛异常:
关闭:new Client(['http_errors' => false]),自己判断 getStatusCode()。
7.3 重试(指数退避,retry)
关闭:'retry' => false。
8. Cookie 会话(CookieJar)
CookieJar 常用方法:
Set-Cookie解析器支持:引号内含分号的 value(例如tk="abc;foo=bar"; Path=/api),不会被错误切断。
单次请求也可以直接传非 Jar 的 cookies:
9. sink 下载、on_stats 统计、调试
9.1 文件下载(sink)
使用 sink 后,$resp->getBody() 会被清空(返回空字符串),避免内存保留一份。
9.2 on_stats 统计回调
9.3 debug:输出 HTTP 原始报文
9.4 全局 http_* 快捷函数
这些函数会复用同一个静态 Client 实例。
10. 可测试:Mock + 历史记录
11. 自定义中间件 & HandlerStack
每个中间件签名:callable(RequestInterface, array): (ResponseInterface|FutureInterface)。
12. 选项速查表
| 选项 | 类型 | 说明 |
|---|---|---|
base_uri |
string / UriInterface | 相对 URI 基准 |
handler |
HandlerStack / HandlerInterface | 自定义底层或中间件栈 |
timeout / connect_timeout |
float | 总 / 连接超时秒(<1 可用) |
verify |
bool / string | false 关校验,或 CA 文件/目录 |
cert / ssl_key |
string / [path, pass] |
客户端证书/私钥 |
proxy |
string / [http,https,no] |
代理;no 为不走代理的域名数组 |
force_ip_resolve |
'v4' / 'v6' / null |
强制 IPv4/6 |
decode_content |
bool | 默认 true:自动压缩&解压 |
auth |
[u,p] / [t,'','bearer'] / string |
Authorization 头 |
headers |
array | 合并进请求头 |
query |
array / string | 追加到 URI query |
json |
mixed | JSON 请求体 |
form_params |
array | URL 编码表单 |
multipart |
array | [{name, contents, filename?, headers?}, ...] |
body |
string / resource / StreamInterface | 原始请求体 |
cookies |
CookieJar / array / string | Jar=会话保持;其他=单次 |
allow_redirects |
bool / [max,strict,keep_body] |
重定向控制 |
http_errors |
bool | 默认 true,4xx/5xx 抛异常 |
retry |
false / [retries,delay,multiplier,when?] |
指数退避重试 |
on_stats |
callable(object) | 请求完成统计回调 |
sink |
string / resource | 响应体直接写文件/流 |
debug |
bool / resource | 原始 HTTP 报文输出 |
curl |
[CURLOPT_* => v] |
CurlHandler 直接追加 CURLOPT |
13. 目录结构
版本要求
- PHP ^8.0(用到构造函数属性提升、
match、str_contains/str_starts_with/str_ends_with、nullsafe?->、??=、mixed类型等) ext-curl、ext-json(composer 自动校验)
All versions of http-client with dependencies
ext-json Version *
ext-curl Version *