php-guzzleHow to handle a RequestException with PHP Guzzle?
RequestException is an exception thrown when an error is encountered while processing an HTTP request. It can be handled with PHP Guzzle by using a try-catch block.
try {
$client->request('GET', '/');
} catch (RequestException $e) {
echo $e->getMessage();
}
The above code will catch any RequestException and print the error message.
Code explanation
try {
: Start of the try-catch block$client->request('GET', '/');
: Make a GET request to the root URLcatch (RequestException $e) {
: Catch any RequestExceptionecho $e->getMessage();
: Print the error message
Helpful links
More of Php Guzzle
- How to use PHP Guzzle to make a batch request?
- How to set a user agent in PHP Guzzle?
- How to post form data with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to get the response body in PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- How to send multipart requests with Guzzle in PHP?
- How to use cookies with Guzzle in PHP?
- How to make HTTP request with PHP Guzzle?
See more codes...