guzzle https get exception code example
Example 1: guzzle http try catch
try {
$result = theMethodMayThrowException();
} catch (\GuzzleHttp\Exception\RequestException $e) {
if ($e->hasResponse()) {
$response = $e->getResponse();
var_dump($response->getStatusCode());
var_dump($response->getReasonPhrase());
var_dump((string) $response->getBody());
var_dump(json_decode((string) $response->getBody()));
var_dump($response->getHeaders());
var_dump($response->hasHeader('Content-Type'));
var_dump($response->getHeader('Content-Type')[0]);
}
}
Example 2: guzzle catch exceptions
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) {
echo "Internet, DNS, or other connection error\n";
die;
}
catch (RequestException $e) {
echo "Request Exception\n";
die;
}