Swoole Coroutine HTTP Client

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();
Attribute list
  • errCode

echo socket_strerror($client->errCode)

  • body
<?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->get(string $path)

Init the 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.1);
        }
    }
});
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 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.com';
$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('/static/files/swoole-logo.svg', __DIR__ . '/logo.svg');