PHPackages                             yiqiniu/easyhttp - 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. yiqiniu/easyhttp

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

yiqiniu/easyhttp
================

EasyHttp 是一个轻量级、语义化、对IDE友好的HTTP客户端，支持常见的HTTP请求、异步请求和并发请求，让你可以快速地使用 HTTP 请求与其他 Web 应用进行通信。

v1.9.1(2y ago)1433MITPHPPHP &gt;=7.1.0

Since Mar 22Pushed 2y agoCompare

[ Source](https://github.com/yiqiniu/easyhttp)[ Packagist](https://packagist.org/packages/yiqiniu/easyhttp)[ Docs](https://github.com/yiqiniu/easyhttp)[ RSS](/packages/yiqiniu-easyhttp/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (1)Versions (11)Used By (0)

EasyHttp 是一个轻量级、语义化、对IDE友好的HTTP客户端，支持常见的HTTP请求、异步请求和并发请求，让你可以快速地使用 HTTP 请求与其他 Web 应用进行通信。

> EasyHttp并不强制依赖于cURL，如果没有安装cURL，EasyHttp会自动选择使用PHP流处理，或者你也可以提供自己的发送HTTP请求的处理方式。

如果您觉得EasyHttp对您有用的话，别忘了给点个赞哦^\_^ ！

github:[github.com/gouguoyin/easyhttp](https://github.com/gouguoyin/easyhttp "github.com/gouguoyin/easyhttp")

gitee:[gitee.com/gouguoyin/easyhttp](https://gitee.com/gouguoyin/easyhttp "gitee.com/gouguoyin/easyhttp")

安装说明
====

[](#安装说明)

#### 环境依赖

[](#环境依赖)

- PHP &gt;= 5.5.0
- 如果使用PHP流处理，allow\_url\_fopen 必须在php.ini中启用。
- 如果使用cURL处理，cURL &gt;= 7.19.4，并且编译了OpenSSL 与 zlib。

#### 一键安装

[](#一键安装)

```
composer require gouguoyin/easyhttp

```

发起请求
----

[](#发起请求)

#### 同步请求

[](#同步请求)

###### 常规请求

[](#常规请求)

```
$response = Http::get('http://httpbin.org/get');

$response = Http::get('http://httpbin.org/get?name=gouguoyin');

$response = Http::get('http://httpbin.org/get?name=gouguoyin', ['age' => 18]);

$response = Http::post('http://httpbin.org/post');

$response = Http::post('http://httpbin.org/post', ['name' => 'gouguoyun']);

$response = Http::patch(...);

$response = Http::put(...);

$response = Http::delete(...);

$response = Http::head(...);

$response = Http::options(...);
```

###### 发送 Content-Type 编码请求

[](#发送-content-type-编码请求)

```
// application/x-www-form-urlencoded(默认)
$response = Http::asForm()->post(...);

// application/json
$response = Http::asJson()->post(...);
```

###### 发送 Multipart 表单请求

[](#发送-multipart-表单请求)

```
$response = Http::asMultipart(
    'file_input_name', file_get_contents('photo1.jpg'), 'photo2.jpg'
)->post('http://test.com/attachments');

$response = Http::asMultipart(
    'file_input_name', fopen('photo1.jpg', 'r'), 'photo2.jpg'
)->post(...);

$response = Http::attach(
    'file_input_name', file_get_contents('photo1.jpg'), 'photo2.jpg'
)->post(...);

$response = Http::attach(
    'file_input_name', fopen('photo1.jpg', 'r'), 'photo2.jpg'
)->post(...);
```

> 表单enctype属性需要设置成 multipart/form-data

###### 携带请求头的请求

[](#携带请求头的请求)

```
$response = Http::withHeaders([
    'x-powered-by' => 'gouguoyin'
])->post(...);
```

###### 携带重定向的请求

[](#携带重定向的请求)

```
// 默认
$response = Http::withRedirect(false)->post(...);

$response = Http::withRedirect([
    'max'             => 5,
    'strict'          => false,
    'referer'         => true,
    'protocols'       => ['http', 'https'],
    'track_redirects' => false
])->post(...);
```

###### 携带认证的请求

[](#携带认证的请求)

```
// Basic认证
$response = Http::withBasicAuth('username', 'password')->post(...);

// Digest认证(需要被HTTP服务器支持)
$response = Http::withDigestAuth('username', 'password')->post(...);
```

###### 携带 User-Agent 的请求

[](#携带-user-agent-的请求)

```
$response = Http::withUA('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36')->post(...);
```

###### 携带Token令牌的请求

[](#携带token令牌的请求)

```
$response = Http::withToken('token')->post(...);
```

###### 携带认证文件的请求

[](#携带认证文件的请求)

```
$response = Http::withCert('/path/server.pem', 'password')->post(...);
```

###### 携带SSL证书的请求

[](#携带ssl证书的请求)

```
// 默认
$response = Http::withVerify(false)->post(...);

$response = Http::withVerify('/path/to/cert.pem')->post(...);
```

###### 携带COOKIE的请求

[](#携带cookie的请求)

```
$response = Http::withCookies(array $cookies, string $domain)->post(...);
```

###### 携带协议版本的请求

[](#携带协议版本的请求)

```
$response = Http::withVersion(1.1)->post(...);
```

###### 携带代理的请求

[](#携带代理的请求)

```
$response = Http::withProxy('tcp://localhost:8125')->post(...);

$response = Http::withProxy([
    'http'  => 'tcp://localhost:8125', // Use this proxy with "http"
    'https' => 'tcp://localhost:9124', // Use this proxy with "https",
    'no'    => ['.com.cn', 'gouguoyin.cn'] // Don't use a proxy with these
])->post(...);
```

###### 设置超时时间(单位秒)

[](#设置超时时间单位秒)

```
$response = Http::timeout(60)->post(...);
```

###### 设置延迟时间(单位秒)

[](#设置延迟时间单位秒)

```
$response = Http::delay(60)->post(...);
```

###### 设置并发次数

[](#设置并发次数)

```
$response = Http::concurrency(10)->promise(...);
```

#### 异步请求

[](#异步请求)

```
use Gouguoyin\EasyHttp\Response;
use Gouguoyin\EasyHttp\RequestException;

Http::getAsync('http://easyhttp.gouguoyin.cn/api/sleep3.json', ['token' => TOKEN], function (Response $response) {
    echo '异步请求成功，响应内容：' . $response->body() . PHP_EOL;
}, function (RequestException $e) {
    echo '异步请求异常，错误码：' . $e->getCode() . '，错误信息：' . $e->getMessage() . PHP_EOL;
});
echo json_encode(['code' => 200, 'msg' => '请求成功'], JSON_UNESCAPED_UNICODE) . PHP_EOL;

//输出
{"code":200,"msg":"请求成功"}
异步请求成功，响应内容：{"code":200,"msg":"success","second":3}

Http::getAsync('http1://easyhttp.gouguoyin.cn/api/sleep3.json', function (Response $response) {
    echo '异步请求成功，响应内容：' . $response->body() . PHP_EOL;
}, function (RequestException $e) {
    echo '异步请求异常，错误信息：' . $e->getMessage() . PHP_EOL;
});
echo json_encode(['code' => 200, 'msg' => '请求成功'], JSON_UNESCAPED_UNICODE) . PHP_EOL;

//输出
{"code":200,"msg":"请求成功"}
异步请求异常，错误信息：cURL error 1: Protocol "http1" not supported or disabled in libcurl (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

Http::postAsync(...);

Http::patchAsync(...);

Http::putAsync(...);

Http::deleteAsync(...);

Http::headAsync(...);

Http::optionsAsync(...);

使用 等待异步回调处理完成
Http::wait();
```

#### 异步并发请求

[](#异步并发请求)

```
use Gouguoyin\EasyHttp\Response;
use Gouguoyin\EasyHttp\RequestException;

$promises = [
    Http::getAsync('http://easyhttp.gouguoyin.cn/api/sleep3.json'),
    Http::getAsync('http1://easyhttp.gouguoyin.cn/api/sleep1.json', ['name' => 'gouguoyin']),
    Http::postAsync('http://easyhttp.gouguoyin.cn/api/sleep2.json', ['name' => 'gouguoyin']),
];

Http::concurrency(10)->multiAsync($promises, function (Response $response, $index) {
    echo "发起第 $index 个异步请求，请求时长：" . $response->json()->second . '秒' . PHP_EOL;
}, function (RequestException $e, $index) {
    echo "发起第 $index 个请求失败，失败原因：" . $e->getMessage() . PHP_EOL;
});

//输出
发起第 1 个请求失败，失败原因：cURL error 1: Protocol "http1" not supported or disabled in libcurl (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
发起第 2 个异步请求，请求时长：2 秒
发起第 0 个异步请求，请求时长：3 秒
```

> 如果未调用concurrency()方法，并发次数默认为$promises的元素个数，$promises数组里必须是异步请求

使用响应
----

[](#使用响应)

发起请求后会返回一个 Gouguoyin\\EasyHttp\\Response $response的实例，该实例提供了以下方法来检查请求的响应：

```
$response->body() : string;
$response->json() : object;
$response->array() : array;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->headers() : array;
$response->header($header) : string;
```

异常处理
----

[](#异常处理)

请求在发生客户端或服务端错误时会抛出 Gouguoyin\\EasyHttp\\RequestException $e异常，该实例提供了以下方法来返回异常信息：

```
$e->getCode() : int;
$e->getMessage() : string;
$e->getFile() : string;
$e->getLine() : int;
$e->getTrace() : array;
$e->getTraceAsString() : string;
```

更新日志
----

[](#更新日志)

### 2022-05-11

[](#2022-05-11)

- 新增removeBodyFormat() 用于withOptions 指定body时，清除原由的bodyFromat
-

### 2022-05-10

[](#2022-05-10)

- 新增发送原生请求的方法client()
- 新增发送原生异步请求的方法clientASync()
-

### 2021-01-23

[](#2021-01-23)

- 修复上传的asMultipart错误的bug
- 新增 wait() 用于等待异步请求的加调

### 2020-03-30

[](#2020-03-30)

- 修复部分情况下IDE不能智能提示的BUG
- get()、getAsync()方法支持带参数的url
- 新增withUA()方法
- 新增withStream()方法
- 新增asMultipart()方法，attach()的别名
- 新增multiAsync()异步并发请求方法

### 2020-03-20

[](#2020-03-20)

- 新增异步请求getAsync()方法
- 新增异步请求postAsync()方法
- 新增异步请求patchAsync()方法
- 新增异步请求putAsync()方法
- 新增异步请求deleteAsync()方法
- 新增异步请求headAsync()方法
- 新增异步请求optionsAsync()方法

Todo List
---------

[](#todo-list)

- 异步请求
- 并发请求
- 重试机制
- 支持http2
- 支持swoole

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.6% 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 ~141 days

Recently: every ~234 days

Total

10

Last Release

967d ago

PHP version history (2 changes)v1.0PHP &gt;=5.5.0

v1.2PHP &gt;=7.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/f0aa2fff4cdc0e0d00811f8ce13874e5e168c805011623fcb1bbe6b7172ff148?d=identicon)[gjianbo](/maintainers/gjianbo)

---

Top Contributors

[![kuafuRace](https://avatars.githubusercontent.com/u/13517412?v=4)](https://github.com/kuafuRace "kuafuRace (66 commits)")[![gjianbo2020](https://avatars.githubusercontent.com/u/71360566?v=4)](https://github.com/gjianbo2020 "gjianbo2020 (12 commits)")

---

Tags

httpphpcurlphp-httpphphttpeasy-httpEasyHttp

### Embed Badge

![Health badge](/badges/yiqiniu-easyhttp/health.svg)

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

###  Alternatives

[yzh52521/easyhttp

EasyHttp 是一个轻量级、语义化、对IDE友好的HTTP客户端，支持常见的HTTP请求、异步请求和并发请求，让你可以快速地使用 HTTP 请求与其他 Web 应用进行通信。

429.3k3](/packages/yzh52521-easyhttp)[gouguoyin/easyhttp

EasyHttp 是一个轻量级、语义化、对IDE友好的HTTP客户端，支持常见的HTTP请求、异步请求和并发请求，让你可以快速地使用 HTTP 请求与其他 Web 应用进行通信。

361.1k](/packages/gouguoyin-easyhttp)[smi2/phpclickhouse

PHP ClickHouse Client

83510.1M71](/packages/smi2-phpclickhouse)[zounar/php-proxy

Forward your HTTP/HTTPS requests to another server.

1814.0k](/packages/zounar-php-proxy)[popphp/pop-http

Pop Http Component for Pop PHP Framework

1018.5k13](/packages/popphp-pop-http)

PHPackages © 2026

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