sorry to ask a question to which there are already many answers but I followed the suggestions given here Catch Guzzle exceptions on connections refused without being able to solve. I keep getting the PHP error Uncaught GuzzleHttp\Exception\ConnectException even though I added the catch. This is my code:
use \GuzzleHttp\Client;
use \GuzzleHttp\Exception\ConnectException;
use \GuzzleHttp\Exception\RequestException;
and
public function send_post_request( $endpoint, $json ) {
$http = $this->client;
try {
$res = $http->request( 'POST', $endpoint, [
'verify' => false,
'timeout' => 120,
'json' => $json
] );
$status = $res->getStatusCode();
$body = $res->getBody()->getContents();
$response = [
'status' => $status,
'body' => json_decode( $body, true )
];
return $response;
} catch ( ConnectException $e ) {
$error = [
'status' => 404,
'body' => $e->getMessage(),
];
return $error;
} catch ( RequestException $e ) {
$error = [
'status' => $e->getResponse()->getStatusCode(),
'body' => json_decode( $e->getResponse()->getBody(true)->getContents(), true ),
];
return $error;
} catch ( \Exception $e ) {
$error = [
'status' => 0,
'body' => $e->getMessage(),
];
return $error;
}
}
The RequestException is caught correctly instead the ConnectException always results in the error:
Uncaught GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: ifconfig.me
or
Uncaught GuzzleHttp\Exception\ConnectException: cURL error 28: Failed to connect to ifconfig.me port 443
Could you please tell me in your opinion what is wrong with the code? Thank you Giulia