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

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->setDefer(bool $defer = false)

Parameters

defer

If the Client should be set in defer mode or not.

Return

None


Description

Set the client to be in defer mode, allow the other coroutine enabled requests to execute when waiting for the result. This improves concurrency and wastes less time when waiting for connections to return data.


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]);

    // Other coroutines can execute while waiting for requests
    $client->setDefer(true);

    $client->get('/index.php');

    var_dump($client->body);

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