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

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

Declaration

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

Parameters

cookies

Set the cookies for the HTTP request.

Return

None


Description

Set the cookies for the HTTP request.

Cookie values will be parsed through urlencode. Must always be a key-value array.

  • Cookies after being set are preserved during the lifetime of the client object
  • Cookies can be obtained by reading $client->cookies
  • Repeated calls to setCookies() will overwrite any values previously set

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

    // Setup cookies using an array, key-value format
    $client->setCookies([
        'a' => 'b',
        'auth_token' => 'HdwejDNjdfweifjhHJfjjJdhUUe...',
    ]);

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

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