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

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\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 OpenSwoole\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('OpenSwoole');

    // 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 September 1, 2022