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

Declaration

<?php
Swoole\Coroutine\Http\Client->setBasicAuth(string $username, string $password): bool

Parameters

username

The username for the HTTP basic auth.

password

The password for the HTTP basic auth.

Return

When successful true is returned or false if something went wrong.

Description

Set the HTTP basic auth username and password for the HTTP request.

For more information on how basic HTTP authentication works, see the Mozilla documentation for it.

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' => 2]);

    $client->setBasicAuth('user', 'pass');

    $client->get('/account/user/auth');

    echo $client->body;

    $client->close();
});