PHPackages                             verdient/http - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. verdient/http

ActiveLibrary[HTTP &amp; Networking](/categories/http)

verdient/http
=============

php http client

2.1.0(3mo ago)01.3k↓25%6MITPHPPHP ^8.3

Since Feb 5Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Verdient/http)[ Packagist](https://packagist.org/packages/verdient/http)[ Docs](https://github.com/Verdient/http)[ RSS](/packages/verdient-http/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (29)Used By (6)

HTTP Client
===========

[](#http-client)

HTTP 客户端

创建新的请求实例
--------

[](#创建新的请求实例)

```
use Verdient\Http\Request;

$request = new Request();
```

设置请求参数
------

[](#设置请求参数)

```
$request->setUrl(string $url); //设置请求URL
$request->setMethod(string $method); //设置请求方法
$request->setHeaders([$name => $value, ...]); //设置请求头部
$request->setQueries([$name => $value, ...]); //设置查询参数
$request->setBodies([$name => $value, ...]); //设置消息体参数
$request->setProxy(string $address, ?int $port = null); //设置代理
$request->setTimeout(int $timeout); //设置超时时间
```

添加参数
----

[](#添加参数)

`Header`, `Query`, `Body`均支持添加参数，相应方法为：

```
- addHeader(string $key, string|array $value)
- addQuery(string $name, string|array $value)
- addBody(string $name, mixed $value)
```

设置消息体序列化器
---------

[](#设置消息体序列化器)

```
use Verdient\Http\Serializer\Body\JsonBodySerializer;
$request->setBodySerializer(new JsonBodySerializer);
```

可通过实现`Verdient\Http\Serializer\Body\BodySerializerInterface`来实现任意的消息体序列化

设置查询参数序列化器
----------

[](#设置查询参数序列化器)

```
use Verdient\Http\Serializer\Query\RFC1738Serializer;
$request->setQuerySerializer(new RFC1738Serializer);
```

可通过实现`Verdient\Http\Serializer\SerializerInterface`来实现任意的查询参数序列化

直接设置消息体
-------

[](#直接设置消息体)

若消息体格式并非 Key-Value 格式或其他需要直接设置消息体的情况，可以直接调用

```
$request->setContent(string $content);
```

`setContent`与`setBody`为互斥关系，调用的`setContent`会清除`setBody`设置的内容，反之亦然

发送请求
----

[](#发送请求)

```
$result = $request->send();
```

响应结果
----

[](#响应结果)

```
# 请求返回结果对象，可获取部分常用的数据

$result->getIsOK(): bool; //获取请求是否成功
$result->getRequest(): Request; //获取请求对象
$result->getErrorCode(): ?int // 获取错误码
$result->getErrorMessage(): ?string // 获取错误信息
$result->getStatusCode(): ?int // 获取HTTP状态码
$result->getHeaders(): ?array // 获取头部响应数据
$result->getBodies(): ?array // 获取HTTP消息体响应数据

# 可以通过getResponse获取响应对象，来实现更多的操作
$response = $result->getResponse(): Response; //获取响应对象

$response->getRawResponse(): string; //获取响应原文
$response->getRawStatus(): string; //获取状态行原文
$response->getRawHeaders(): string; //获取头部原文
$response->getRawContent(): ?string; //获取消息体原文
$response->getStatusCode(): int; //获取状态码
$response->getHeaders(): array; //获取解析后的头部
$response->getBodies(): mixed; //获取解析后的消息体参数
$response->getCookies(): array; //获取解析后的Cookie
$response->getContentType(): ?string; //获取消息体类型
$response->getCharset(): ?string; //获取字符集
$response->getStatusMessage(): string; //获取状态消息
$response->getHttpVersion(): string; //获取HTTP版本
$response->getRequest(): Request; //获取请求对象
```

批量请求
----

[](#批量请求)

```
use Verdient\Http\BatchRequest;

/**
 * 请求对象的集合
 * 集合内的元素必须是Request的实例
 */
$requests = [];

for($i = 0; $i < 100; $i++){
    $request = new Request();
    $request->setUrl($url);
    $request->addQuery('id', $i);
    $requests[] = $request;
}

/**
 * 批大小，默认为100
 */
$batchSize = 100;

$batch = new BatchRequest($requests, $batchSize);

/**
 * 返回内容为数组，keyValue对应关系与构造BatchRequest时传入的数组相同
 * 遍历返回的结果，结果与Request调用send方法后返回的内容一致，使用方法也相同
 */
$result = $batch->send();
```

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance78

Regular maintenance activity

Popularity19

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~80 days

Recently: every ~189 days

Total

28

Last Release

116d ago

Major Versions

0.4.5 → 1.0.62023-08-18

1.0.9 → 2.0.02025-06-04

PHP version history (2 changes)0.0.0PHP &gt;=7.0.0

2.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/606eaa4bc61b11fa19f25526a3084758b7086ceb8b11cdbcafd5df11871f2437?d=identicon)[Verdient。](/maintainers/Verdient%E3%80%82)

---

Top Contributors

[![Verdient](https://avatars.githubusercontent.com/u/22468867?v=4)](https://github.com/Verdient "Verdient (23 commits)")

---

Tags

httprequestrestful

### Embed Badge

![Health badge](/badges/verdient-http/health.svg)

```
[![Health](https://phpackages.com/badges/verdient-http/health.svg)](https://phpackages.com/packages/verdient-http)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

7.9k1.0B3.2k](/packages/guzzlehttp-psr7)[psr/http-message

Common interface for HTTP messages

7.1k1.0B5.5k](/packages/psr-http-message)[psr/http-factory

PSR-17: Common interfaces for PSR-7 HTTP message factories

1.9k692.9M1.9k](/packages/psr-http-factory)[nategood/httpful

A Readable, Chainable, REST friendly, PHP HTTP Client

1.8k17.2M267](/packages/nategood-httpful)[fig/http-message-util

Utility classes and constants for use with PSR-7 (psr/http-message)

39489.0M274](/packages/fig-http-message-util)[nette/http

🌐 Nette Http: abstraction for HTTP request, response and session. Provides careful data sanitization and utility for URL and cookies manipulation.

48619.2M541](/packages/nette-http)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
