Swoole Coroutine HTTP/WebSocket Client

Swoole\Coroutine\HTTP\Client supports HTTP 1.1, HTTPS and WebSocket. It has to be executed within Coroutine context.

Multiple HTTP requests can be executed concurrently within one thread with Swoole Coroutine HTTP Client.

Swoole\Coroutine\HTTP\Client->__construct(string $host, int port, bool $ssl = false)

Example:

<?php
$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80);
$cli->setHeaders([
    'Host' => "localhost",
    "User-Agent" => 'Chrome/49.0.2587.3',
    'Accept' => 'text/html,application/xhtml+xml,application/xml',
    'Accept-Encoding' => 'gzip',
]);
$cli->set([ 'timeout' => 1]);
$cli->get('/index.php');
echo $cli->body;
$cli->close();
Example
<?php
$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80);
$cli->get('/index.php');
echo $cli->body;
$cli->close();
  • statusCode

The HTTP status code.

Swoole\Coroutine\Http\Client->set(array $settings)

Set multiple setting variables.

Swoole\Coroutine\Http\Client->setDefer(bool $defer)

Set the client to be defer mode, allow the other Coroutine request to execute when waiting the result.

Swoole\Coroutine\Http\Client->getDefer()

Check if the request is in defer mode.

Example of using defer
<?php
$mysql = new Swoole\Coroutine\MySQL();
$mysql_res = $mysql->connect(['host' => '127.0.0.1', 'user' => 'root', 'password' => '', 'database' => 'temp']);
if ($mysql_res == false) {
    echo "MySQL server can't be connected."
    return;
}
$mysql->setDefer(true);
$mysql->query('select sleep(1)', 2);
var_dump($mysql_res);

# execute the other clients
$udp_tmp = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
$udp_res = $udp_tmp->connect("127.0.0.1", 9909, 2);//nonexistent server
$udp_res = $udp_tmp->recv();//get false with timeout
var_dump($udp_res);

$mysql_res = $mysql->recv();
var_dump($mysql_res);

Swoole\Coroutine\Http\Client->setMethod(string $method)

Set the HTTP method of the request.

Swoole\Coroutine\Http\Client->setHeaders(array $headers)

Set multiple HTTP header variables.

Swoole\Coroutine\Http\Client->setBasicAuth(string $username, string $password)

Set multiple HTTP Basic Auth username and password.

Swoole\Coroutine\Http\Client->setCookies(array $cookies)

Set multiple HTTP cookies.

Swoole\Coroutine\Http\Client->setData($data)

Set multiple HTTP post data.

Swoole\Coroutine\Http\Client->addFile(string $path, string $name, string $type, string filename, $type, $offset, $length)

Upload a file to the remote server.

Swoole\Coroutine\Http\Client->addData(string $path, string $name, string $type, string filename)

Set multiple HTTP post data from file.

Swoole\Coroutine\Http\Client->execute(string $path)

Execute the HTTP request.

Swoole\Coroutine\Http\Client->getBody()

Get body of the HTTP response.

Swoole\Coroutine\Http\Client->getHeaders()

Get headers of the HTTP response.

Swoole\Coroutine\Http\Client->getCookies()

Get cookies of the HTTP response.

Swoole\Coroutine\Http\Client->getStatusCode()

Get Status Code of the HTTP response.

Swoole\Coroutine\Http\Client->getStatusCode()

Get Status Code of the HTTP response.

Swoole\Coroutine\Http\Client->get(string $path)

Execute the HTTP GET request.

Example:

<?php

$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80);
$cli->setHeaders([
    'Host' => "localhost",
    "User-Agent" => 'Chrome/49.0.2587.3',
    'Accept' => 'text/html,application/xhtml+xml,application/xml',
    'Accept-Encoding' => 'gzip',
]);

$cli->get('/index.php');
echo $cli->body;
$cli->close();
Swoole\Coroutine\Http\Client->post(string $path, mixed $data)

Init the POST request.

<?php
$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80); 
$cli->post('/post.php', array("a" => '1234', 'b' => '456'));
echo $cli->body;
$cli->close();
Swoole\Coroutine\Http\Client->upgrade(string $path)

Upgrade to be websocket connection.

Example:

<?php
go(function () {
    $cli = new Co\http\Client("127.0.0.1", 9501);
    $ret = $cli->upgrade("/");
    if ($ret) {
        while(true) {
            $cli->push("hello");
            var_dump($cli->recv());
            co::sleep(0.5);
        }
    }
});
Swoole\Coroutine\Http\Client->push(mixed $data, int $opcode = WEBSOCKET_OPCODE_TEXT, bool $finish = true): bool

Push data to the remote websocket server.

Swoole\Coroutine\Http\Client->recv(float $timeout = -1)

Receive data from the remote server.

Example:

<?php
go(function () {
    $cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80);
    $cli->setHeaders([
        'Host' => "localhost",
        "User-Agent" => 'Chrome/49.0.2587.3',
        'Accept' => 'text/html,application/xhtml+xml,application/xml',
        'Accept-Encoding' => 'gzip',
    ]);
    $cli->set([ 'timeout' => 1]);
    $cli->setDefer();
    $cli->get('/');
    co::sleep(1);
    $data = $cli->recv();
});
WebSocket Client example:
<?php
go(function () {
    $cli = new Co\http\Client("127.0.0.1", 9501);
    $ret = $cli->upgrade("/");

    if ($ret) {
        while(true) {
            $cli->push("hello");
            var_dump($cli->recv());
            co::sleep(0.1);
        }
    }
});
Swoole\Coroutine\Http\Client->addFile(string $path, string $name, string $mimeType = null, string $filename = null, int $offset = 0, int $length = 0)

Attach file to the POST request.

Example:

<?php
$cli = new Swoole\Coroutine\Http\Client('httpbin.org', 80);
$cli->setHeaders([
    'Host' => "httpbin.org"
]);
$cli->set(['timeout' => -1]);
$cli->addFile(__FILE__, 'file1', 'text/plain');
$cli->post('/post', ['foo' => 'bar']);
echo $cli->body;
$cli->close();
Swoole\Coroutine\Http\Client->addFile(string $data, string $name, string $mimeType = null, string $filename = null)

Generate file from string and attach to the POST request.

<?php
$cli = new Swoole\Coroutine\Http\Client('httpbin.org', 80);
$cli->setHeaders([
    'Host' => "httpbin.org"
]);
$cli->set(['timeout' => -1]);
$cli->addData(Co::readFile(__FILE__), 'file1', 'text/plain');
$cli->post('/post', ['foo' => 'bar']);
echo $cli->body;
$cli->close();
Swoole\Coroutine\Http\Client->download(string $path, string $filename, int $offset = 0);

Download data from the remote server.

Example:

<?php
$host = 'www.swoole.co.uk';
$cli = new Swoole\Coroutine\Http\Client($host, 443, true);
$cli->set(['timeout' => -1]);
$cli->setHeaders([
    'Host' => $host,
    "User-Agent" => 'Chrome/49.0.2587.3',
    'Accept' => '*',
    'Accept-Encoding' => 'gzip'
]);
$cli->download('/images/swoole-logo.svg', __DIR__ . '/swoole-logo.svg');