Since 2019, here's what I've developed from the above answers and Guzzle documents to handle exceptions, get the response body, status code, message, and other, sometimes useful, response elements.
try { $result = theMethodMayThorwException(); } catch (\Exception $e) { if ($e->hasResponse()) { $response = $e->getResponse(); var_dump($response->getStatusCode());
Voila. You get response information in conveniently divided paragraphs.
Side notes:
Using the catch clause, we catch the exception class of the root exception of the PHP chain \Exception , as custom exceptions in Guzzle extend it.
This approach can be useful in cases where Guzzle is used under the hood, as in the Laravel or AWS API PHP SDK, so you cannot catch a genuine Guzzle exception.
In this case, the exception class may not coincide with the class mentioned in the Guzzle documentation (for example, GuzzleHttp\Exception\RequestException as the root exception for Guzzle).
Therefore, you should catch \Exception , but remember that this is still an instance of the Guzzle exception class.
Use with caution though. These wrappers can make genuine Guzzle $e->getResponse() object methods inaccessible. In this case, you will have to take a look at the actual source code of the shell and figure out how to get the status, message, etc. Instead of using Guzzle $response methods.
If you call Guzzle yourself, you can catch GuzzleHttp\Exception\RequestException or any other mentioned in their exception documents regarding the terms of use.
bob-12345 Jun 30 '19 at 7:24 2019-06-30 07:24
source share