PHP curl timeout error detection

眉间皱痕 提交于 2019-12-30 03:45:08

问题


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?


回答1:


Use curl_errno()

Code 28 is timeout.




回答2:


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);
}


来源:https://stackoverflow.com/questions/11867891/php-curl-timeout-error-detection

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