Open Swoole Async HTTP2 client

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

This class or function have been deprecated.

Http 2.0 client support stream and multiplexing. Multiple GET or POST request can be sent over the same TCP connection.

OpenSwoole HTTP2 client is based on nghttp2, enable this feature when compiling with --enable-nghttp2, --enable-openssl or --with-openssl-dir.

OpenSwoole\HTTP2\Client::\_\_construct($host, $port, $ssl = false)

Alias: swoole_http2_client->__construct($host, $port, $ssl = false)

Construct HTTP2 client.

OpenSwoole\HTTP2\Client->get(string $path, callable $callback)

Alias: swoole_http2_client->get(string $path, callable $callback)

Send GET request, example callback function:

function callback(OpenSwoole\Http2\Response $resp)
OpenSwoole\HTTP2\Client::post(string $path, mixed $data, callable $callback)

Alias: swoole_http2_client->post(string $path, mixed $data, callable $callback)

Send POST request, example:

$cli = new swoole_http2_client('', 80);
$cli->post('/post.php', array("a" => '1234', 'b' => '456'), function ($response) {
    echo "Length: " . strlen($cli->body) . "\n";
    echo $cli->body;
OpenSwoole\HTTP2\Client::setHeaders(array $headers)

Alias: swoole_http2_client->setHeaders(array $headers)

Setup the http request headers.

OpenSwoole\HTTP2\Client::setCookies(array $cookies)

Alias: swoole_http2_client->setCookies(array $cookies)

Setup the http request cookies.

$array = array(
    "host" => "",
    "accept-encoding" => "gzip, deflate",
    'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'accept-language' => 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,ja;q=0.2',
    'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) Chrome/58.0.3026.3 Safari/537.36',

$client = new OpenSwoole\Http2\Client("", 443, true);

$client->setCookies(array("a" => "1", "b" => "2"));

$client->get("/", function ($o) use($client) {
    echo "#{$client->sock} hello world 1\n";
    echo $o->body;

$client->post("/", $array, function ($o) use($client) {
    echo "{$client->sock} hello world 3\n";
    echo $o->body;
Last updated on September 1, 2022