OpenSwoole Async HTTP/Websocket client

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

This class or function have been deprecated.

OpenSwoole Async HTTP client is a high performance and aync HTTP client supports Http-Chunk, Keep-Alive, form-data.

Events
  • connect
  • message
  • close
  • error
Swoole\Http\Client::\_\_construct ( string $host [, string $port [, boolean $ssl ]] )

Alias: swoole_http_client::__construct

Example:

<?php
Swoole\Async::dnsLookup("www.google.com", function ($domainName, $ip) {
    $cli = new swoole_http_client($ip, 80);
    $cli->setHeaders([
        'Host' => $domainName,
        "User-Agent" => 'Chrome/49.0.2587.3',
        'Accept' => 'text/html,application/xhtml+xml,application/xml',
        'Accept-Encoding' => 'gzip',
    ]);
    $cli->get('/index.html', function ($cli) {
        echo "Length: " . strlen($cli->body) . "\n";
        echo $cli->body;
    });
});
void Swoole\Http\Client::set ( array $settings )

Alias: swoole_http_client::set

Set the parameters for the http client.

Example:

<?php
// Set the connect timeout
$http->set(['timeout' => 3.0]);
// Set the keep alive option
$http->set(['keep_alive' => false]);
// Set websocket mask
$http->set(['websocket_mask' => true]);
void Swoole\Http\Client::setMethod ( string $method )

Alias: swoole_http_client::setMethod

Set the http request method.

Example:

<?php
$client->setMethod("PUT");
void Swoole\Http\Client::setHeaders ( array $headers )

Alias: swoole_http_client::setHeaders

Set the http request headers.

void Swoole\Http\Client::setCookies ( array $cookies )

Alias: swoole_http_client::setCookies

Set the http request cookies.

void Swoole\Http\Client::setData ( string $data )

Alias: swoole_http_client::setData

Set the http request body data. The http method will be changed to be POST.

void Swoole\Http\Client::addFile ( string $path , string $name [, string $type [, string $filename [, string $offset ]]] )

Alias: swoole_http_client::addFile

Add files to the post form.

Example:

<?php

<?php
$cli = new swoole_http_client('127.0.0.1', 80);
//post request
$cli->setHeaders(['User-Agent' => "swoole"]);
$cli->addFile(__DIR__.'/post.data', 'post');
$cli->addFile(dirname(__DIR__).'/test.jpg', 'debug');
$cli->post('/dump2.php', array("xxx" => 'abc', 'x2' => 'rango'), function ($cli) {
    echo $cli->body;
});
void Swoole\Http\Client::on ( string $event_name , callable $callback )

Alias: swoole_http_client::on

Register callback function by event name.

void Swoole\Http\Client::get ( string $path , callable $callback )

Alias: swoole_http_client::get

Send GET http request to the remote server.

Example:

<?php

$cli = new swoole_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', function ($cli) {
    echo "Length: " . strlen($cli->body) . "\n";
    echo $cli->body;
});
void Swoole\Http\Client::post ( string $path , string $data , callable $callback )

Alias: swoole_http_client::post

Send POST http request to the remote server.

Example:

<?php

$cli = new swoole_http_client('127.0.0.1', 80);
$cli->post('/post.php', array("a" => '1234', 'b' => '456'), function ($cli) {
    echo "Length: " . strlen($cli->body) . "\n";
    echo $cli->body;
});
void Swoole\Http\Client::upgrade ( string $path , string $callback )

Alias: swoole_http_client::upgrade

Upgrade to websocket protocol.

<?php
$cli = new swoole_http_client('127.0.0.1', 9501);

$cli->on('message', function ($_cli, $frame) {
    var_dump($frame);
});

$cli->upgrade('/', function ($cli) {
    echo $cli->body;
    $cli->push("hello world");
});
// Websocket callback function
function onMessage(swoole_http_client $client, swoole_websocket_frame $frame);
void Swoole\Http\Client::execute ( string $path , string $callback )

Alias: swoole_http_client::execute

Send the HTTP request after setting the parameters.

void Swoole\Http\Client::download ( string $path , string $file , callable $callback [, int $offset ] )

Alias: swoole_http_client::download

Download file from the remote server.

Example:

<?php

$cli = new swoole_http_client('127.0.0.1', 80);

$cli->setHeaders([
    'Host' => "localhost",
    "User-Agent" => 'Chrome/49.0.2587.3',
    'Accept' => '*',
    'Accept-Encoding' => 'gzip',
]);

$cli->download('/video.avi', __DIR__.'/video.avi', function ($cli) {
    var_dump($cli->downloadFile);
});

// download by range
$cli = new swoole_http_client('127.0.0.1', 80);
$file = __DIR__.'/video.avi';
$offset = filesize($file);
$cli->setHeaders([
    'Host' => "localhost",
    "User-Agent" => 'Chrome/49.0.2587.3',
    'Accept' => '*',
    'Accept-Encoding' => 'gzip',
    'Range' => "bytes=$offset-",
]);

$cli->download('/video.avi', $file, function ($cli) {
    var_dump($cli->downloadFile);
}, $offset);
void Swoole\Http\Client::push ( string $data [, string $opcode [, string $finish ]] )

Alias: swoole_http_client::push

Push data to websocket client.

boolean Swoole\Http\Client::isConnected ( void )

Class alias: swoole_http_client::isConnected

Check if the http connection is connected.

void Swoole\Http\Client::close ( void )

Class alias: swoole_http_client::close

Close the http connection.

Last updated on August 31, 2022