7

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

use GuzzleHttp\Psr7\Request;

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

And save them in an array: $guzzleRequests

I then create a pool to simultaneously execute all requests:

    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 the Doc: http://guzzle.readthedocs.org/en/latest/quickstart.html under "Concurrent requests")

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

How do I add authentication to the Request so that the Pool can run multiple requests against Basic HTTP Authorization protected APIs at the same time?

*edit 1:

In response to pinkal vansia: I added the header as you suggested:

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

and dumped 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 response still yields unauthorized:

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

* final edit:

I finally got it running. It turns out, pinkal vansia was already pretty close.

The exact form was the last problem. Michael Downling's comment brought me on the right track.

The Authorization header is the way to go and it needs to be a key => value mapping.

The final 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;
7
  • What kind of authentication do you need to add? Commented Jun 25, 2015 at 15:47
  • Basic HTTP Authentication, using a password and a username. Originally I would have done it using $request = client::get()->setAuth(). Before sending the request out: $request->send(). So if you are used to the old syntax, that is what I want to rebuild using the new syntax. Commented Jun 26, 2015 at 11:21
  • Here, originally we had that: return $this->guzzleClient->createRequest('GET', $url, [ 'auth' => [$this->username, $this->password], 'query' => $this->queryArray ]); This does not exist anymore. We need to re-create it in the new way of doing it. Commented Jun 26, 2015 at 11:30
  • 2
    Headers are a hash of header name to value. Don't specify the headers as a line in a single string but rather a key mapping to a value. Commented Jun 29, 2015 at 21:42
  • @MichaelDowling your comment did show me the way in the end, after another hour of fiddling with it. Fixing this problem took me over 2 days to get to the bottom of it and even more time fixing it now. Since it was simply a formal problem, providing a single example of the right form would have saved me hours of work. You would have been able to provide this easily. I understand you are a busy person and I am a beginner, but please may I ask you to make it easier for the next beginner, asking a stupid question, trying to learn the framework you are coding. Commented Jul 1, 2015 at 13:46

1 Answer 1

8

You can add Basic Authentication header in Request as 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 header needs to be a key => value pair. so final solution is like below,

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

$myRequest = new Request(
    'GET',
     $url,
     $headers
);
Sign up to request clarification or add additional context in comments.

6 Comments

I edited my original question. Comments didn't allow for proper formatting.
I have updated my answer. I have changed the creation of request.Please try it.
I have Edited my Answer. Now i am setting 'header' in options array as per github.com/guzzle/guzzle/blob/master/src/Client.php
This returns a Promise instead of a Request, leading the Pool to respond with "Each value yielded by the iterator must be a Psr7\Http\Message\RequestInterface or a callable that returns a promise that fulfills with a Psr7\Message\Http\ResponseInterface object." in line 61 of GuzzleHttp\Pool.
Your solution was almost perfect. I added the "final edit" to my question. It includes the correct form. Would you update your answer accordingly, so I can mark it as answer?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.