Swoole\Coroutine\Http\Client->execute(...)

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Coroutine\Http\Client->execute(string $path): int|bool

Parameters

path

The URL path to send a HTTP request to. For example /user/account or /index.php. Only set the path here not the protocol or domain name like http://domain.

Return

Success

Returns the HTTP status code as an int if the request was made without any client errors.

Failure

Returns false if something went wrong, use $client->errCode to see what went wrong.


Description

Execute a HTTP request based on using setMethod and setData.

This method is very similar to post() or get() but it allows you to have more control over how the request is built before being sent.

You must call setMethod and or addData before making a request with execute


Example

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

Co\run(function()
{
    $client = new Client('127.0.0.1', 80);

    $client->setHeaders([
        'Host' => "localhost",
        "User-Agent" => 'Chrome/49.0.2587.3',
        'Accept' => 'text/html,application/xhtml+xml,application/xml',
        'Accept-Encoding' => 'gzip',
    ]);

    $client->set(['timeout' => 1]);

    // Defer HTTP requests and allow other coroutines to execute when waiting
    $client->setDefer(true);

    // Set the request type and add a file using a string
    $client->setMethod('POST');
    $client->setData('swoole');

    // Execute client request based on how it was setup...
    $status = $client->execute('/index.php');

    echo "Request status: " . $status . "\n";

    var_dump($client->getBody());

    $client->close();
});
Last updated on August 31, 2022