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

流过昼夜 提交于 2020-01-02 05:04:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29617480/how-to-handle-fatal-error-curl-error-7-failed-to-connect-to-xxxx-port-443

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!