PHP CURL中CURLOPT_CONNECTTIMEOUT_MS超时问题
写了一个简单的curl请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
$data = curl_exec($ch);
curl_close($ch);
再A服务器运行没有然后问题,到了B服务器发现$data返回false 这就令我百思不得其解了? 来我们开始排坑
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
$data = curl_exec($ch);
if($data === false){
print_r('---------------------start------------------------'.PHP_EOL);
var_dump(curl_errno($ch));
var_dump(curl_error($ch));
print_r('---------------------end------------------------');
exit();
}
curl_close($ch);
然后我们发现它告诉我们errno:28 超时了!!! 超时了那好解决啊,想当然的我把超时时间从200ms提升倒了600ms,要知道我们这个接口测试的时候是直接绑定host,响应也是直接返回一个json数据.600ms绝对足够了.
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 600);
还是超时! 这就有点不对劲了! 继续修改时间
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
成功了!那么问题就出在CURLOPT_TIMEOUT_MS上面了!
原来 CURLOPT_CONNECTTIMEOUT_MS 的等待的时间,以毫秒为单位。 真确的开启方式是
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
|