guzzle http try catch

try {
    /**
     * We use Guzzle to make an HTTP request somewhere in the
     * following theMethodMayThrowException().
     */
    $result = theMethodMayThrowException();
} catch (\GuzzleHttp\Exception\RequestException $e) {
    /**
     * Here we actually catch the instance of GuzzleHttp\Psr7\Response
     * (find it in ./vendor/guzzlehttp/psr7/src/Response.php) with all
     * its own and its 'Message' trait's methods. See more explanations below.
     *
     * So you can have: HTTP status code, message, headers and body.
     * Just check the exception object has the response before.
     */
    if ($e->hasResponse()) {
        $response = $e->getResponse();
        var_dump($response->getStatusCode()); // HTTP status code;
        var_dump($response->getReasonPhrase()); // Response message;
        var_dump((string) $response->getBody()); // Body, normally it is JSON;
        var_dump(json_decode((string) $response->getBody())); // Body as the decoded JSON;
        var_dump($response->getHeaders()); // Headers array;
        var_dump($response->hasHeader('Content-Type')); // Is the header presented?
        var_dump($response->getHeader('Content-Type')[0]); // Concrete header value;
    }
}
// process $result etc. ...

4
2
Pouya Ataei 105 points

                                    use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;

$client = new Client();

try{
	$response = $client->request('GET', 'http://github.com');
}
catch (ConnectException $e) {
	// Connection exceptions are not caught by RequestException
	echo "Internet, DNS, or other connection error\n";
    die;
}
catch (RequestException $e) {
	echo "Request Exception\n";
    die;
}
// deal with your $reponse here

4 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
handle guzzle exception error $request client dentro catch guzzle try catch guzzle catch serverException guzzle php guzzle http error handling guzzle request without exception how to get error try catch guzzle php guzzle request exception guzzlehttp catch exception and get status guzzle http see error handling Guzzle exception response guzzle Http response throw guzzle request options exceptions false guzzle catch client error try catch guzzle request 7 try catch guzzle request guzzlehttp catch errors try catch guzzle get error code command breaks on guzzle request even on try catch laravel guzzle catch http_errors guzzle new client http_errors get status of response in catch guzzle guzzle 7 php try catch guzzle php try catch guzzle http get error handling statuscode guzzle https get exception guzzle client exception get status code guzzle http_errors guzzle try catch not working guzzle catch 204 in try catch guzzle catch 200 http_errors guzzle how to handle GuzzleHttp client exception guzzle try catch exception status guzzle try catch exception guzzle ignore http error handling guzzle http error handling guzzle get exception code guzzle get error code guzzle handle exception manually guzzle catch any guzzle exception get response body GUZZLE CATCH ERROR how to catch error in guzzlehttp client guzzlehttp error handling try catch guzzlehttp exception clientexception guzzle exception getcode guzzle client error response guzzle exception get response guzzle error handling get Guzzle Client exception message try catch GuzzleHttp laravel try catch GuzzleHttp guzzle client exception guzzle catch exceptions guzzle exception handling guzzle http try catch guzzle http exception guzzle exceptions GuzzleHttp try catch laravel handle response error in guzzle GuzzleHttp client exceptions GuzzleHttp exceptions guzzle get all exception response guzzle client exception get response guzzle request exception get status code try catch php laravel guzzlehttp client expection try catch php laravel guzzlehttp } catch guzzle handle guzzle exceptions guzzle exception request exception guzzle guzzle handle exceptions catch guzzle exceptions how to check error in guzzle laravel guzzle catch clientexception guzzle try catch laravel try catch for guzzle exception handling best practice why thrwo exception GuzzleHttp\Client throw guzzle client exception with code throw guyyle client exception with code catch guzzle error catch guzzlehttp exception clientexception try catch guzzle laravel Undefined type 'GuzzleHttp\Exception\BadResponseException guzzle not catching 502 error handling 500 errors in guzzle
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source