curl如果需要进行毫秒超时,需要增加:
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
或者是:
curl_setopt ( $ch, CURLOPT_NOSIGNAL, true); 是可以支持毫秒级别超时设置的
[codes=php]
<?php
if (!isset($_GET['foo'])) {
// Client
$ch = curl_init('http://example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
$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.";
}
?>
[/code]
来自:http://developer.51cto.com/art/201208/351996_2.htm
curl的超时时间 :
今天在一台服务器上突然看到一个curl进程已经运行了28天还木结束, 有点奇怪! 我在使用curl的时候也设置了超时时间, --connect-timeout 5
curl --connect-timeout 5 --data-binary "set=${L_UPLOAD_DATA_ENCODED}" http://172.88.99.00:8080/xxx.php &>/dev/null
按理来说, 应该是5s就会超时退出了! 纳闷之余想起wget好像对超时时间, 是有分阶段的, 比如说请求的超时, 传输的超时等等, 所以就仔细查看了下curl的手册页:
原来使用curl时,有两个超时时间:一个是连接超时时间,另一个是整个过程允许的最大时间,
--connect-timeout <seconds>
Maximum time in seconds that you allow the connection to the server to take. This only limits the connection phase, once curl has connected this option is of no more use. See also the -m/--max-time option.
If this option is used several times, the last one will be used.
这个是指定连接超时时间。 如果出错, 提示形如:curl: (28) connect() timed out!
-m/--max-time <seconds>
Maximum time in seconds that you allow the whole operation to take. This is useful for preventing your batch jobs from hanging for hours due to slow networks or links going down. See also the --connect-timeout option.
If this option is used several times, the last one will be used.
这个是指定最大的允许时间。 出错提示如:curl: (28) Operation timed out after 2000 milliseconds with 0 bytes received
还可以这样用: curl -o x.log "http://www.yyyy.com" --speed-time 5 --speed-limit 1
是说将url内容保存到x.log中, 如果传输速度小于1字节/秒的状态持续5秒,该连接就会终止.
来自:http://blog.chinaunix.net/uid-20788470-id-1841681.html
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/6616/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2013-8-27 17:43
评论列表