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
- 青云互联:香港荃湾云服务器限时七折,13元/月起;江苏挂机宝vps低至5 香港VPS[主机]
- 硅云香港服务器怎么样?看网友们是怎么评价的 香港VPS[主机]
- 为什么大家都选择香港服务器建站 香港VPS[主机]
- 原创解析:云计算含义及定义 全球[VPS测评]
- Vultr新用户100美金 最新活动来啦!新用户充值活动来了,充100美 全球[VPS测评]
- 狗云DogYun,香港cn2云服务器/优惠码/流量包,按小时计费云服务器 香港VPS[主机]
- 亿恩科技香港云服务器 2核2G服务器特价700元/年 香港VPS[主机]
- 桔子数据怎么样?便宜香港安畅CN2 VPS1核1G10M 28/月 香港VPS[主机]
- btcvps,比特云怎么样?香港VPS,KVM架构带宽4M月付88港币 香港VPS[主机]
- 企鹅小屋:1核/512MB/10GB SSD空间/500GB流量/KVM 虚拟空间(主机)
- JGKVM:1核/512M/30G/600G/30Mbps/洛杉矶CN2 全球[VPS测评]
- 品光网络怎么样?香港美国服务器/云/高防,开年采购3.5折起 美国VPS[主机]
- sugarhosts:虚拟主机3折(有独立IP),云服务器5折,香港\\ 独立服务器[U]
- tmhhost官网登录不上怎么办?日本软银/美国CN2 GIA/美国高防 日本VPS[主机]
- 青云互联:香港弹性云CN2直连线路,全场8折,免费windows系统,月 WINDOWS
- 腾讯云香港/韩国/日本免备案服务器优惠促销活动 日本VPS[主机]
- 云米科技:香港/美国/日本等海外免备案vps云服务器,最低13元/月 日本VPS[主机]
- UCloud快杰型海外云主机:可选香港/上海/北京/日本/新加坡/韩国/ 日本VPS[主机]
- 水墨云:特惠香港、日本、韩国、洛杉矶小内存vps;IPLC专线/海外CN 日本VPS[主机]
- 魔方云:香港原生IP vps/大带宽,月付79元起;美国洛杉矶CN2 G 美国VPS[主机]
- dogyun:香港/韩国/美国cn2定制VPS,静态云服务器终身8折,低 韩国VPS[主机]
- pigyun:韩国CN2云服务器仅14元;香港CN2建站vps特惠+美国 韩国VPS[主机]
- HostKvm:洛杉矶云主机/香港云主机八折优惠,大带宽,月付5.2美元 香港VPS[主机]
- 创意IT服务公司主页网站模板 - Softets 全球[VPS测评]
- 快速云:怎么连接vps?VPS的用途有哪些? 全球[VPS测评]
- Rustrot-深绿色UI电商网站模板HTML5框架 全球[VPS测评]
- “休闲养蜂”风靡海外跨境电商追逐垂直风口 全球[VPS测评]
- 华凯易佰同比扭亏跨境电商行业回暖H2旺季将至中报追踪 全球[VPS测评]
- 腾讯云香港轻量应用服务器 优惠活动最低月付仅24元 香港VPS[主机]
- UCloud香港云主机怎么样?UCloud香港云主机1核1G1M评测 香港VPS[主机]
转载请注明原文地址: http://140.238.13.167:12355/read-220635.html











