How to handle Fatal error: cURL 7 error: Failed to connect to port xxxx 443

I have a script that connects to a third-party API. It works and should work on a continuous 24/7 cycle (I use sleep at the end before restarting the cycle).

The problem is that sometimes a third-party API gets ddosed or the connection just crashes with this error:

Fatal error: exception for exception 'GuzzleHttp \ Ring \ Exception \ ConnectException' with the message 'cURL error 7: Could not connect to port xxx.com 443

Is there a way to โ€œcrashโ€ this fatal error to make sure the code restarts and continues to work if the action can be completed, or should I manually restart every time I get this error?

+4
source share
1 answer

From michael comment

it looks like you can just catch the GuzzleHttp \ Ring \ Exception \ ConnectException exception

like this:

use GuzzleHttp\Ring\Exception\ConnectException;

try {
    // the code which throws the error
} catch( ConnectException $ex ) {
    switch ( $ex->getMessage() ) {
        case '7': // to be verified
            // handle your exception in the way you want,
            // maybe with a graceful fallback
            break;
    }
}

a guzzle ConnectException extends several classes appears and eventually extends php Exception so that you can safely use the getCode () method, allowing you to catch an identifier that you can respond to according to your needs.

+6
source

All Articles