curl CURLOPT_TIMEOUT_MS 小于 1 秒无效的解决方案
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

CURLOPT_TIMEOUT 设置 cURL 允许执行的最长秒数
CURLOPT_TIMEOUT_MS 设置 cURL 允许执行的最长毫秒数
CURLOPT_CONNECTTIMEOUT 在发起连接前等待的时间,如果设置为 0,则无限等待
CURLOPT_CONNECTTIMEOUT_MS 尝试连接等待的时间,以毫秒为单位。如果设置为 0,则无限等待
CURLOPT_TIMEOUT_MS 在 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: 增加 curl_setopt($ch, CURLOPT_NOSIGNAL, 1) 可以解决此问题: if (!isset($_GET['foo'])) { // Client $ch = curl_init('http://localhost/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_errorn"; } else { echo "Data received: $datan"; } } else { // Server sleep(10); echo "Done."; }猜你可能想看的VPS
- JGKVM 五一劳动节专场促销,香港 cmi 日本 cn2 美国 cn2日本VPS[主机]
- CentOS7 服务器 LAMP 配置 怎么在 CentOS 上安装 L全球[VPS测评]
- raksmart 美国大陆优化服务器带宽速度及综合性能测评,raksma独立服务器[U]
- 腾讯云 - 域名续费减 20 代金券 云服务器满 200-100 代金券全球[VPS测评]
- 6yun→45 元 月 256MB 内存 5GB 空间 2TB 流量 1虚拟空间(主机)
- 便宜 VPS $15 年 2 核 CUP 2G 内存 50G 硬盘 5T全球[VPS测评]
- $2 月 512M 内存 10G SSD 1Gbps 不限流量 KVM 全球[VPS测评]
- 腾讯云”共克时艰 续费扶持”,史无前例的腾讯云服务器老用户 3.6 折续全球[VPS测评]
- racknerd→16C 站群服务器(256IP) e3-1230v2 站群服务器[IP]
- 用ImgURL免费图床程序自建WordPress图床全球[VPS测评]
- WordPress 网站自定义广告位占位/出租代码全球[VPS测评]
- friendhosting黑五促销,VDS四五折,虚拟主机四折;老用户续全球[VPS测评]
- 百度智能云云服务器特惠新用户1核2G/40G硬盘/1Mbps带宽 ¥95全球[VPS测评]
- centOS7系统下如何修改服务器SSH端口默认的22。全球[VPS测评]
- 2021年最新宝塔面板如何关闭强制绑定账号全球[VPS测评]
- 2022年GigsGigsCloud最新优惠码,推荐香港/洛杉矶/日本C日本VPS[主机]
- KhanWebHost 伦敦不限流量2G便宜VPS月付4.5美元起;1G全球[VPS测评]
- HKServerSolution → 2G 内存 20G HDD 2T单全球[VPS测评]
- Wikihost → 洛杉矶CERA Gen2 Lite 联通回程 →全球[VPS测评]
- 又有美议员窜访台,中国驻美大使馆:“中方将对美国的挑衅采取果断的反制措施美国VPS[主机]
- 快速云:云服务器vps的区别是什么云服务器和vps哪个比较好2022-0全球[VPS测评]
- 40火伞高压电爬坡更让企业用电于民全球[VPS测评]
- 辰迅云怎么样?国内香港云服务器全场5折,物理服务器限时特价香港VPS[主机]
- VPS到底有什么用?全球[VPS测评]
- 56云服务器怎么样?56云免备案香港云服务器价格多少钱?香港VPS[主机]
- 阿里云服务器一年价格多少钱?阿里云服务器报价全球[VPS测评]
- 二三互联,香港cn2云服务器5折+85折双重优惠,稳定不限流量,1核1G香港VPS[主机]
- 磐逸云:深圳/大连/成都/香港机房vps,1核1G3M仅26元/月,65香港VPS[主机]
- 无忧云怎么样?大连BGP德阳100G高防香港CN2VPS云服务器香港VPS[主机]
- LiCloud:香港CN2线路云服务器,100Mbps大带宽,6折优惠中香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-220635.html











