Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

curl

guzzle

I have a script that connects to a third party API. It is and should be running on a non-stop loop 24/7 (I use a sleep at the end before restarting the loop).

The problem is that sometimes the third party API gets ddosed or the connection simply drops with this error:

Fatal error: Uncaught exception 'GuzzleHttp\Ring\Exception\ConnectException' with message 'cURL error 7: Failed to connect to xxx.com port 443

Is there any way to "break" on this fatal error to ensure the code is restarted and proceed if the action can be made or must I manually restart each time I get this error?

like image 933
ace Avatar asked Apr 14 '15 00:04

ace


People also ask

Why curl error 7 failed to connect to port 443?

Conclusion In short, cURL error 7 failed to connect to port 443 mainly occurs when the firewall blocks the curl request. Today, we have discussed this error in detail and saw how our Support Engineers fix it for our customers.

What is curl error 7?

What is cURL error 7? Have any idea about when does the curl error occur? Curl error 7 mainly occurs when the TCP connection to a given host/port fails. It ends up in an error message. PHP Fatal error: Uncaught exception 'Exception' with message 'cURL error: [7] Failed to connect to xxx.paypal.com port 443: Connection refused'.

Is port 443 open in TCP in/out or UDP out?

Also, we found port 443 opened in TCP In/Out and UDP Out for both IPv4 and IPv6. How we fix cURL error 7 failed to connect to port 443?

Why do I get a curl error on my server?

This mainly occurs when the server firewall blocks the curl request. At Bobcares, we often receive requests to fix this error as part of our Server Management Services. Today, let’s discuss the error in detail and see how our Support Engineers fix it for our customers.


1 Answers

From Michael's 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;
    }
}

it appears guzzle's ConnectException extends some classes and ultimately extends php's Exception so you can safely use the getCode() method, allowing you to catch an identifier on which you can react accordingly to your needs.

like image 69
Félix Adriyel Gagnon-Grenier Avatar answered Oct 03 '22 03:10

Félix Adriyel Gagnon-Grenier