PHP curl 超时错误检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11867891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:18:39  来源:igfitidea点击:

PHP curl timeout error detection

phpcurlerror-handlingtimeout

提问by morteza kavakebi

I use curl to perform a HTTP request like this:

我使用 curl 来执行这样的 HTTP 请求:

$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

$data = curl_exec($ch);
curl_close($ch);

How can I check if an error occurred, and whether it was a timeout error?

如何检查是否发生错误,以及是否是超时错误?

回答by Jason McCreary

Use curl_errno()

curl_errno()

Code 28is timeout.

代码 28是超时。

回答by morteza kavakebi

you can check error number and its' description like this:

您可以像这样检查错误号及其描述:

// Check if any error occurred
if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_error($ch);
}