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 set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- What version of PHP is required for Guzzle?
- How to make HTTP request with PHP Guzzle?
- How to stream with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
See more codes...