歡迎您光臨本站 註冊首頁

Guzzle-Swoole v2.0.0,Guzzle 協程化,支持 ElasticSearch

←手機掃碼閱讀     admin @ 2020-01-13 , reply:0

介紹

讓 Guzzle 支持 Swoole 協程,這個項目目的就是這麼簡單明了!

Guzzle-Swoole 是 Guzzle 的處理器(Handler),並沒有對 Guzzle 本身代碼進行修改,理論上可以兼容後續版本。

v2.0.0 更新日誌

* 新增 Ring Handler 支持,可以用於 elasticsearch/elasticsearch 等包中:

* 無需再手動引入 guzzle

* 使用 YurunHttp 作為 Http 請求驅動,更加穩定可靠

使用說明

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

全局設定處理器


<?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目錄下代碼。

ElasticSearch


$client = \Elasticsearch\ClientBuilder::create()->setHosts(['192.168.0.233:9200'])->setHandler(new \Yurun\Util\Swoole\Guzzle\Ring\SwooleHandler())->build();

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

Github:https://github.com/Yurunsoft/Guzzle-Swoole


[admin ]

來源:OsChina
連結:https://www.oschina.net/news/112761/guzzle-swoole-2-released
Guzzle-Swoole v2.0.0,Guzzle 協程化,支持 ElasticSearch已經有182次圍觀

http://coctec.com/news/soft/show-post-222883.html