Skip to content

reactphp-x/guzzle-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

$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!

作者

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages