marscoo / Guzzle-Swoole

让基于 Guzzle 的项目完美无缝兼容 Swoole 协程

Home Page:https://gitee.com/yurunsoft/Guzzle-Swoole

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Guzzle-Swoole

Latest Version Php Version Swoole Version IMI License

介绍

让 Guzzle 支持 Swoole 协程,这个项目目的就是这么简单明了!

Guzzle-Swoole 是 Guzzle 的处理器(Handler),并没有对 Guzzle 本身代码进行修改,理论上可以兼容后续版本。

QQ群:17916227 点击加群

使用说明

Composer:"yurunsoft/guzzle-swoole":"~1.1"

本项目不包含 Guzzle 功能,请自行在项目中引用 Guzzle 6.x,理论上可以支持升级 Guzzle 版本而无需更新 Guzzle-Swoole!

全局设定处理器

<?php
require dirname(__DIR__) . '/vendor/autoload.php';

use GuzzleHttp\Client;
use Yurun\Util\Swoole\Guzzle\SwooleHandler;
use GuzzleHttp\DefaultHandler;

DefaultHandler::setDefaultHandler(SwooleHandler::class);

go(function(){
    $client = new Client();
    $response = $client->request('GET', 'http://www.baidu.com', [
        'verify'    =>  false,
    ]);
    var_dump($response->getStatusCode());
});

手动指定 Swoole 处理器

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Yurun\Util\Swoole\Guzzle\SwooleHandler;

go(function(){
    $handler = new SwooleHandler();
    $stack = HandlerStack::create($handler);
    $client = new Client(['handler' => $stack]);
    $response = $client->request('GET', 'http://www.baidu.com', [
        'verify'    =>  false,
    ]);
    var_dump($response->getBody()->__toString(), $response->getHeaders());
});

更加详细的示例代码请看test目录下代码。

About

让基于 Guzzle 的项目完美无缝兼容 Swoole 协程

https://gitee.com/yurunsoft/Guzzle-Swoole

License:MIT License


Languages

Language:PHP 100.0%