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
- 折扣 hostdare 上新货了 CN2 GIA+三网直连+大硬盘 VP全球[VPS测评]
- IMIDC→$9 月 KVM-512MB 20GB 300GB 南非 C全球[VPS测评]
- invs→4.1 元 月 512MB 内存 5GB 空间 不限流量 10虚拟空间(主机)
- 设置 select 某一项不可选 禁用 select 指定 option全球[VPS测评]
- WindowsVPS→$45 年 3G 内存 30GB SSD 空间 不WINDOWS
- ZJI→独立服务器 6 折起 香港 CN2 日本 CN2 韩国 CN2 独立服务器[U]
- 线路不行-HostDoc→£4.99 月 8GB 内存 30GB SSD虚拟空间(主机)
- NameCheap - .me 域名注册优惠 仅需$3.88 年全球[VPS测评]
- 新商家慎重–XenSpec→$2.4 月 KVM-1GB 10GB 无限全球[VPS测评]
- linux(ubuntu centos)下开启 mysql 远程访问权限全球[VPS测评]
- 80vps 五一劳动节特惠,韩国 LG 洛杉矶高防 vps 特惠五折,1韩国VPS[主机]
- B2CHOST→$5 月 6 核 4GB 内存 200GB 空间 4TB虚拟空间(主机)
- 高德地图 点赞好司机 领现金红包全球[VPS测评]
- WordPress插件:The Paste方便图片粘贴上传全球[VPS测评]
- 知更鸟Begin主题代码实现外链转内链并base64加密全球[VPS测评]
- 什么是阿里云 ECS 服务器全球[VPS测评]
- centOS7系统下如何修改服务器SSH端口默认的22。全球[VPS测评]
- EUGameHost 新加坡1£VPS 测试全球[VPS测评]
- Neterra → 5.99€ 月 1C1G10G硬盘 保加利亚 不限流全球[VPS测评]
- GreenCloudVPS → 美国欧洲存储型 → 2G 500G 美国VPS[主机]
- 2023年 IDC市场结构和方向的深度分析全球[VPS测评]
- 中行升级服务力促跨境电商发展全球[VPS测评]
- kvmcloud怎么样?香港BGP云服务器 月付12.9元香港VPS[主机]
- 阁天互联怎么样?香港cn2云服务器 圣何塞cn2vps仅20/月香港VPS[主机]
- 又有美议员窜访台,中国驻美大使馆:“中方将对美国的挑衅采取果断的反制措施美国VPS[主机]
- 漂亮的Css3响应侧边栏菜单,动画风格侧边栏导航菜单样式全球[VPS测评]
- btcvps,比特云怎么样?香港VPS,KVM架构带宽4M月付88港币香港VPS[主机]
- 搬瓦工:美国香港cn2 gia线路VPS,10Gbps带宽,移动+联通企美国VPS[主机]
- 亿速云“双11”上云狂欢大促 高防云服务器、裸金属服务器优惠套餐全球[VPS测评]
- 尊云双十二:国内4核4G5M云服务器仅28元/月,香港CN2VPS仅84香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-208707.html











