As per in the : Docs
You can handle the exception
GuzzleHttpExceptionClientException - 400 errors
GuzzleHttpExceptionServerException - 500 errors
or you can use the super class of it
GuzzleHttpExceptionBadResponseException
You can use try catch
$client = new GuzzleHttpClient;
try {
$res = $client->get($url);
if ($res->getStatusCode() == 200) {
$j = $res->getBody();
$obj = json_decode($j);
$forecast = $obj->hourlyForecasts->forecastLocation;
}
} catch (GuzzleHttpExceptionBadResponseException $e) {
// handle the response exception
$forecast = null;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…