我有一个连接到第三方 API 的脚本。它现在并且应该在 24/7 不间断循环中运行(我在重新启动循环之前在最后使用 sleep )。
问题是有时第三方 API 被关闭或连接因此错误而简单地断开:
Fatal error: Uncaught exception 'GuzzleHttp\Ring\Exception\ConnectException' with message 'cURL error 7: Failed to connect to xxx.com port 443
是否有任何方法可以“中断”这个 fatal error 以确保代码重新启动并在可以执行操作时继续,或者我必须在每次收到此错误时手动重新启动?
最佳答案
it looks like you can just catch the GuzzleHttp\Ring\Exception\ConnectException exception
像这样:
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;
}
}
它似乎是 guzzle 的 ConnectException extends some classes并最终扩展 php 的 Exception因此您可以安全地使用 getCode() 方法,允许您捕获一个标识符,您可以根据您的需要做出相应的 react 。
关于php - 如何处理 fatal error : cURL error 7: Failed to connect to xxxx port 443,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29617480/