PHPackages                             reactphp-x/guzzle-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. reactphp-x/guzzle-http

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

reactphp-x/guzzle-http
======================

00PHP

Since Jun 30Pushed 10mo agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

ReactPHP X Guzzle HTTP Adapter
==============================

[](#reactphp-x-guzzle-http-adapter)

一个将 Guzzle HTTP 客户端与 ReactPHP 集成的适配器，支持异步和同步两种模式。

特性
--

[](#特性)

- 🚀 基于 ReactPHP 的高性能异步 HTTP 客户端
- 🔄 支持异步和同步两种调用模式
- 🎯 完全兼容 Guzzle HTTP 客户端接口
- ⚙️ 灵活的配置选项
- 🛡️ 完整的错误处理和异常转换

安装
--

[](#安装)

```
composer require reactphp-x/guzzle-http
```

基本用法
----

[](#基本用法)

### 异步模式（默认）

[](#异步模式默认)

```
use ReactphpX\GuzzleHttp\HttpClientAdapter;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

// 创建适配器（默认异步模式）
$adapter = new HttpClientAdapter();
$client = new Client(['handler' => $adapter]);

$request = new Request('GET', 'https://api.example.com/data');

// 异步调用
$promise = $client->sendAsync($request);
$promise->then(
    function ($response) {
        echo "请求成功: " . $response->getStatusCode();
    },
    function ($exception) {
        echo "请求失败: " . $exception->getMessage();
    }
);
```

### 同步模式

[](#同步模式)

```
// 创建同步模式适配器
$adapter = new HttpClientAdapter(null, ['async' => false]);
$client = new Client(['handler' => $adapter]);

$request = new Request('GET', 'https://api.example.com/data');

// 同步调用
try {
    $response = $client->send($request);
    echo "请求成功: " . $response->getStatusCode();
} catch (Exception $e) {
    echo "请求失败: " . $e->getMessage();
}
```

### 运行时切换模式

[](#运行时切换模式)

```
$adapter = new HttpClientAdapter();
$client = new Client(['handler' => $adapter]);

$request = new Request('GET', 'https://api.example.com/data');

// 异步调用
$asyncPromise = $client->sendAsync($request, ['async' => true]);

// 同步调用
$syncResponse = $client->send($request, ['async' => false]);
```

配置选项
----

[](#配置选项)

### 全局配置

[](#全局配置)

```
$adapter = new HttpClientAdapter(null, [
    'async' => true,                    // 是否使用异步模式
    'timeout' => 30,                    // 请求超时时间（秒）
    'connect_timeout' => 10,            // 连接超时时间（秒）
    'verify' => true,                   // SSL 验证
    'allow_redirects' => true,          // 允许重定向
    'max_redirects' => 5,               // 最大重定向次数
    'http_errors' => true,              // 是否抛出 HTTP 错误
    'headers' => [],                    // 默认请求头
    'protocol_version' => '1.1',        // HTTP 协议版本
    'base_uri' => null,                 // 基础 URI
    'response_buffer' => true,          // 响应缓冲
    'reject_error_response' => true,    // 拒绝错误响应
]);
```

### 请求级别配置

[](#请求级别配置)

```
$response = $client->send($request, [
    'async' => false,                   // 覆盖全局异步设置
    'timeout' => 60,                    // 覆盖超时设置
    'headers' => ['X-Custom' => 'value'], // 添加自定义头部
    'query' => ['param' => 'value'],    // 添加查询参数
    'auth' => ['username', 'password'], // 基本认证
]);
```

高级用法
----

[](#高级用法)

### 代理支持

[](#代理支持)

```
$adapter = new HttpClientAdapter(null, [
    'proxy' => 'http://proxy.example.com:8080'
]);
```

### Unix Domain Socket

[](#unix-domain-socket)

```
$adapter = new HttpClientAdapter(null, [
    'unix_socket' => '/var/run/docker.sock'
]);
```

### 自定义连接器

[](#自定义连接器)

```
use React\Socket\Connector;

$connector = new Connector(['timeout' => 5]);
$adapter = new HttpClientAdapter(null, [
    'connector' => $connector
]);
```

错误处理
----

[](#错误处理)

适配器会自动将 ReactPHP 的异常转换为 Guzzle 的异常类型：

- `ConnectException`: 连接错误
- `RequestException`: 请求错误
- `TransferException`: 传输错误

```
try {
    $response = $client->send($request);
} catch (GuzzleHttp\Exception\ConnectException $e) {
    // 处理连接错误
} catch (GuzzleHttp\Exception\RequestException $e) {
    // 处理请求错误
} catch (GuzzleHttp\Exception\TransferException $e) {
    // 处理传输错误
}
```

性能优化
----

[](#性能优化)

### 异步模式优势

[](#异步模式优势)

- 非阻塞 I/O
- 高并发处理能力
- 内存使用效率高

### 同步模式优势

[](#同步模式优势)

- 代码逻辑简单直观
- 适合简单的脚本和工具
- 易于调试和测试

注意事项
----

[](#注意事项)

1. **异步模式**：需要确保事件循环正在运行
2. **同步模式**：会阻塞当前线程直到请求完成
3. **内存管理**：大量并发请求时注意内存使用
4. **错误处理**：始终使用 try-catch 或 Promise 处理异常

许可证
---

[](#许可证)

MIT License

贡献
--

[](#贡献)

欢迎提交 Issue 和 Pull Request！

作者
--

[](#作者)

- wpjscc ()

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/76907477?v=4)[wpjscc](/maintainers/wpjscc)[@wpjscc](https://github.com/wpjscc)

---

Top Contributors

[![wpjscc](https://avatars.githubusercontent.com/u/76907477?v=4)](https://github.com/wpjscc "wpjscc (2 commits)")

### Embed Badge

![Health badge](/badges/reactphp-x-guzzle-http/health.svg)

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

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78026.4M414](/packages/react-http)[php-http/curl-client

PSR-18 and HTTPlug Async client with cURL

48347.0M384](/packages/php-http-curl-client)[smi2/phpclickhouse

PHP ClickHouse Client

84310.1M71](/packages/smi2-phpclickhouse)

PHPackages © 2026

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