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

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

Declaration

<?php OpenSwoole\Coroutine\Http\Client->setHeaders(array $headers): void

Parameters

headers

Set the HTTP headers of the request.

Return

None


Description

Set the HTTP headers of the request.

The client uses a key-value array format and the HTTP standard of $key: $value. When headers are set, they become permanent between requests and life only for the lifetime of the client object. You may set the headers again to overwrite any previous key-value pairs.


Example

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

co::run(function()
{
    $client = new Client('127.0.0.1', 80);

    // Setup request headers
    $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]);

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

    $client->close();
});
Last updated on September 1, 2022