How to add authentication to GuzzleHTTP request objects for asynchronous processing

I am creating several of the following GuzzleHttp \ Psr7 \ Requests:

use GuzzleHttp\Psr7\Request;

$myRequest = new Request(
    'GET',
    $someUri
);

      

And store them in an array: $ guzzleRequests

Then I create a pool to execute all requests at the same time:

    use GuzzleHttp\Pool;

    $testPool = new Pool($testClient = new \GuzzleHttp\Client(), $guzzlePromises,
    [
        'fulfilled' => function ($response, $index) {
            // this is delivered each successful response
            var_dump($response);
        },
        'rejected' => function ($reason, $index) {
            // this is delivered each failed request
            var_dump($reason);
        }
    ]);
    // Initiate the transfers and create a promise
    $promise = $testPool->promise();

    // Force the pool of requests to complete.
    $promise->wait();

      

(Taken from Doc: http://guzzle.readthedocs.org/en/latest/quickstart.html under Parallel Queries)

This works for URI requests that don't need authentication and returns 200 OK Status.

How can I add authentication to the request so that the pool can run multiple requests at the same time against the core HTTP protected APIs?

* edit 1:

In response to rose vance : I added a header as you suggested:

$headers = [
    'Authorization: Basic '. base64_encode($this->username.':'.$this->password),
];
$myRequest = new Request(
    'GET',
    $url,
    $headers
);`

      

and dropped the headers:

array (size=2)
    'Host' => 
    array (size=1)
        0 => string '<myHost>' (length=27)
0 => 
    array (size=1)
        0 => string 'Authorization: Basic <veryLongAuthenticationString>' (length=<stringLength>)`

      

The answer is still unauthorized:

private 'reasonPhrase' => string 'Unauthorized' (length=12)
private 'statusCode' => int 401

      

* final edit:

Finally I got it running. Turns out pinkal vansia is already pretty close.

The exact shape was the last issue. Michael Downling 's comment got me on the right track.

The authorization header is the navigation method and it should be a key mapping =>.

The last thing looks like this:

$url = $myUrl.'?'.http_build_query($this->queryArray);

// ------------ Specify Authorization => key to make it work!!!
$headers = [
    'Authorization' => 'Basic '. base64_encode($this->username.':'.$this->password)
];
// -----------------------------------------------------------

$myRequest = new Request(
    'GET',
    $url,
    $headers
);

return $myRequest;

      

+3


source to share


1 answer


You can add Basic Authentication header to Request as shown below

$headers = [
    'Authorization: Basic '. base64_encode($this->username.':'.$this->password)
];

$myRequest = new Request(
    'GET',
    $url,
    $headers
);

      

I hope this helps.



UPDATE

As @worps pointed out, there header

should be a couple key => value

. so the final solution is like below,

$headers = [
    'Authorization' => 'Basic '. base64_encode($this->username.':'.$this->password)
];

$myRequest = new Request(
    'GET',
     $url,
     $headers
);

      

+3


source







All Articles