PHP cURL 的超时设置有两个 CURLOPT_CONNECTTIMEOUT 和 CURLOPT_TIMEOUT,他们的区别是:

CURLOPT_CONNECTTIMEOUT 用来告诉 PHP 在成功连接服务器前等待多久(连接成功之后就会开始缓冲输出),这个参数是为了应对目标服务器的过载,下线,或者崩溃等可能状况。
CURLOPT_TIMEOUT 用来告诉成功 PHP 从服务器接收缓冲完成前需要等待多长时间,如果目标是个巨大的文件,生成内容速度过慢或者链路速度过慢,这个参数就会很有用。
使用 cURL 下载 MP3 文件是一个对开发人员来说不错的例子,CURLOPT_CONNECTTIMEOUT 可以设置为10秒,标识如果服务器10秒内没有响应,脚本就会断开连接,CURLOPT_TIMEOUT 可以设置为100秒,如果MP3文件100秒内没有下载完成,脚本将会断开连接

 

需要注意的是:CURLOPT_TIMEOUT 默认为0,意思是永远不会断开链接。所以不设置的话,可能因为链接太慢,会把 HTTP 资源用完。

在 WordPress 中,wp_http 类,这两个值是一样的,默认是设置为 5 秒。

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_URL, $config['comet_server'] . $api); //请求的api链接
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);//如果$timeout时间内 没有请求成功,就会断开连接 默认是0 ,一般设置5-10s
$response = curl_exec($ch);
curl_close($ch);

//最后 $respose 返回 ok即为请求成功  我们可以以此为依据,确定是否请求成功

$res = $response === "OK" ? true : false;

 

php的curl函数有多个参数:

CURLOPT_TIMEOUT_MS 设置cURL允许执行的最长毫秒数。  在cURL 7.16.2中被加入。从PHP 5.2.3起可使用。 所以使用的时候请先查看libcurl版本 curl --version

但是这个函数有个bug,如果时间小于1000毫秒也就是1秒的话,会立马报错,查看下面说明

​​  If you want cURL to timeout in less than one second, you can use CURLOPT_TIMEOUT_MS, although there is a bug/"feature" on "Unix-like systems" that causes libcurl to timeout immediately if the value is < 1000 ms with the error "cURL Error (28): Timeout was reached". The explanation for this behavior is:
  "If libcurl is built to use the standard system name resolver, that portion of the transfer will still use full-second resolution for timeouts with a minimum timeout allowed of one second."
  What this means to PHP developers is "You can use this function without testing it first, because you can't tell if libcurl is using the standard system name resolver (but you can be pretty sure it is)"
  The problem is that on (Li|U)nix, when libcurl uses the standard name resolver, a SIGALRM is raised during name resolution which libcurl thinks is the timeout alarm.
  The solution is to disable signals using CURLOPT_NOSIGNAL. Here's an example script that requests itself causing a 10-second delay so you can test timeouts:​​

<?php
if (!isset($_GET['foo'])) {
// Client
$ch = curl_init('http://localhost/test/test_timeout.php?foo=bar');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch); if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
echo "Data received: $data\n";
}
} else {
// Server
sleep(10);
echo "Done.";
}
?>

增加 curl_setopt($ch, CURLOPT_NOSIGNAL, 1)