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
- hkserversolution→台湾动态 VPS 随意切换 IP 7.全球[VPS测评]
- vps-mart→$49 月 E3-1230 32GB 内存 1.5TB全球[VPS测评]
- 大盘机 $2.99 月 2 核 CPU 512M 内存 250G 硬盘 全球[VPS测评]
- 保看奈飞 $18.75 月 2 核 CPU 1.5G 内存 30G SS日本VPS[主机]
- 补货通知 搬瓦工→CN2 GIA 线路$46.87 年机器补货了 速度上全球[VPS测评]
- 便宜 $1.08 月 256M 内存 15G 硬盘 1.5T 流量@1G全球[VPS测评]
- locvps→香港 cn2 VPS 终身 7 折 38 元 月起 Xen香港VPS[主机]
- 微信群找不到了怎么办?全球[VPS测评]
- 疯狂猜成语 图猜成语一个人站在起点和终点之间是什么成语?全球[VPS测评]
- 分享一些支持比特币付款的国外独立服务器商家独立服务器[U]
- VPS 装什么系统网站快 Debian稳定首推全球[VPS测评]
- WordPress博客使用php页面代码实现外链go跳转的方法全球[VPS测评]
- 代码禁用WordPress程序图片裁剪功能全球[VPS测评]
- locvps上架日本大阪vps,软银线路,日本原生IP,月付76元日本VPS[主机]
- WordPress插件:WP User Avatar修改默认头像全球[VPS测评]
- Vultr VPS主机取消 $3.5入门机型及新用户赠送$25全球[VPS测评]
- 用 WEBP 图片格式既利于 SEO 优化又能优化图片大小全球[VPS测评]
- Zemi → 15$ 月 北马其顿 2C2G50G硬盘 无限流量全球[VPS测评]
- 50KVM 波特兰系列机型季付优惠活动全球[VPS测评]
- 修罗云 → 深港IPLC专线 → 1核 256M内存 6G硬盘 35G 全球[VPS测评]
- 亿恩科技香港云服务器 2核2G服务器特价700元/年香港VPS[主机]
- 快速云:云服务器和vps区别是什么云服务器和vps哪个比较安全2022-全球[VPS测评]
- 外贸无界烟台市有多少跨境电商?烟台跨境电商开发平台,烟台跨境电商选品全球[VPS测评]
- edgeNAT:2021元旦vps促销_全场韩国/美国/香港VPS低至7韩国VPS[主机]
- 一万网络双蛋狂欢活动:2核2G5M云服务器仅377.46元/年全球[VPS测评]
- edgeNAT:全场6折优惠,韩国CN2、美国CN2、香港CN2月付36韩国VPS[主机]
- 月神云活动:香港大带宽vps、美国CN2普防、美国原生IP、日本东京首月日本VPS[主机]
- 长三角地区的算力需求规模到底有多大?全球[VPS测评]
- 千古互动:香港vps,1核/1G/20M,月付35元起;挂机宝5元/月起香港VPS[主机]
- 晚点独家丨拼多多9月上线跨境电商平台,首站美国美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-205775.html











