简体   繁体   中英

Using a Swoole \Swoole\Coroutine\Http\Client based HttpClientAdapter for Guzzle - How?

I have tried to implement a Guzzle HandlerStack HttpClientAdapter for Swoole and use it via:

$handler = new \App\Swoole\HttpClientAdapter();
$client = new \GuzzleHttp\Client([
   'handler' => \GuzzleHttp\HandlerStack::create($handler),
]);
$res = $client->request('POST', 'http://localhost/foo');

The handler class looks like this:

namespace App\Swoole;

class HttpClientAdapter
{

    public function __invoke(RequestInterface $request, array $options)
    {
        // [..] init and request modification, path/port extraction removed

        $cli = new \Swoole\Coroutine\Http\Client($ip, $port, $ssl);
        $cli->execute($path);
        $cli->close();

        return new Promise\FulfilledPromise(

            new Psr7\Response($cli->statusCode, $cli->headers, $cli->body)
        );
    }
}

This actually works - but only if i put in a MockServer Proxy in between. Without, i just do not get any data back ... i have tried using different endpoints, but to no avail. Does anybody know how to tackle/debug the problem and/or is there a Swoole Guzzle Adapter out there in the wild?

Well, this work is awesome. But I suggest another httpclient based on swoole coroutine: https://github.com/swlib/saber/blob/master/README-EN.md

It's developed by swoole core developer and you will find it quite look like Guzzle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM