PHP curl timeout error detection

2019-02-07 18:22发布

I use curl to perform a HTTP request like this:

$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?

2条回答
叼着烟拽天下
2楼-- · 2019-02-07 18:41

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);
}
查看更多
戒情不戒烟
3楼-- · 2019-02-07 19:03

Use curl_errno()

Code 28 is timeout.

查看更多
登录 后发表回答