Swoole\Coroutine\Http\Client::__construct

Declaration

<?php
Swoole\Coroutine\Http\Client::__construct(string $host, int port, bool $enableSSL = FALSE)

Parameters

host name

Host name of the remote server or IP address

port

Port of the remote server, normally uses 80 for HTTP service and 443 for HTTPS service.

enableSSL

If enable SSL, ssl should be enabled if the remote service is HTTPS.

Return

Swoole\Coroutine\Http\Client

Coroutine HTTP Client

Description

Create a new HTTP Client within the coroutine context.

Note: HTTPS support is only available if Swoole was compiled with SSL support.

Structure

The structure of the HTTP Client object before execution:

<?php
Swoole\Coroutine\Http\Client Object
(
    [errCode] => 0
    [errMsg] =>
    [connected] => 0
    [host] => 127.0.0.1
    [port] => 80
    [ssl] =>
    [setting] =>
    [requestMethod] =>
    [requestHeaders] =>
    [requestBody] =>
    [uploadFiles] =>
    [downloadFile] =>
    [downloadOffset] => 0
    [statusCode] => 0
    [headers] =>
    [set_cookie_headers] =>
    [cookies] =>
    [body] =>
)

Example

<?php
use Swoole\Coroutine\HTTP\Client;

Co\run(function() {
    $url_info = parse_url('http://httpbin.org/a');
    $host = $url_info['host'];
    $path = $url_info['path'];

    $cli = new Client($host);
    $cli->setHeaders([
        'Host' => $host,
        "User-Agent" => 'Chrome/49.0.2587.3',
        'Accept' => 'text/html,application/xhtml+xml,application/xml',
        'Accept-Encoding' => 'gzip',
    ]);
    $cli->set([ 'timeout' => 1]);
    $cli->get($path);
    echo $cli->body;
    $cli->close();
});