Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the response for rejected concurrent requests in Guzzle

Tags:

guzzle

guzzle6

I am using Guzzle concurrency request tool: http://docs.guzzlephp.org/en/latest/quickstart.html#concurrent-requests

My code is similar to the example code:

use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$client = new Client();

$requests = function ($total) {
    $uri = 'http://127.0.0.1:8126/guzzle-server/perf';
    for ($i = 0; $i < $total; $i++) {
        yield new Request('GET', $uri);
    }
};

$pool = new Pool($client, $requests(100), [
    'concurrency' => 5,
    'fulfilled' => function ($response, $index) {
        // this is delivered each successful response
    },
    'rejected' => function ($reason, $index) {
        // this is delivered each failed request
    },
]);

// Initiate the transfers and create a promise
$promise = $pool->promise();

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

The issue is that some of my requests return responses with 500 HTTP responses, but still sends some contents (e.g. why the error happened). Guzzle unfortunately classes http responses with 500 status codes as 'rejected', and I cannot seem to get the original response, as that parameter does not exist in the rejected function.

I can however access the $reason. In my case it contained a JSON like so:

{
    xdebug: "..."
}

The xdebug property contains HTML as a string that looks like so:

GuzzleHttp\Exception\ServerException: Server error: `GET http://example.com` resulted in a `500 Internal Server Error` response: {"failure_reason":"Useful message"} in [...stacktrace ...]

Although this contains the original response, I cannot easily extract it as its hidden away in HTML, making it very unuseful. I also have no idea how this is set in the first place.

Therefore my question is, how can I access the response for rejected concurrent requests?

like image 330
Yahya Uddin Avatar asked Oct 13 '16 07:10

Yahya Uddin


People also ask

How do I get guzzle response?

As described earlier, you can get the body of a response using the getBody() method. Guzzle uses the json_decode() method of PHP and uses arrays rather than stdClass objects for objects. You can use the xml() method when working with XML data.

How do I get my status code from response guzzle?

You can use the getStatusCode function. $response = $client->request('GET', $url); $statusCode = $response->getStatusCode();

What is guzzle exception?

A GuzzleHttp\Exception\ClientException is thrown for 400 level errors if the http_errors request option is set to true. This exception extends from GuzzleHttp\Exception\BadResponseException and GuzzleHttp\Exception\BadResponseException extends from GuzzleHttp\Exception\RequestException .

What is difference between guzzle and cURL?

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. cURL can be classified as a tool in the "File Transfer" category, while Guzzle is grouped under "Microframeworks (Backend)". cURL and Guzzle are both open source tools.


1 Answers

After some effort I finally managed to answer my own question. The $reason is a GuzzleException.

Therefore we can check what type of exception it is and perform the appropriate logic like so:

[
    ...,
    'rejected' => function ($reason, $index) {
        if ($reason instanceof GuzzleHttp\Exception\ClientException) {
            $body = $reason->getResponse()->getBody();
        }
    },
]

Note that not all GuzzleException has a response. See http://docs.guzzlephp.org/en/latest/quickstart.html#exceptions for more details.

like image 55
Yahya Uddin Avatar answered Sep 21 '22 10:09

Yahya Uddin