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
- VmShell:香港CMI线路300MBps,KVM架构vps,年付免费 香港VPS[主机]
- 人力资源管理系统前端Web模板 - SmartHR 全球[VPS测评]
- 40火伞高压电爬坡更让企业用电于民 全球[VPS测评]
- 如何根据业务需求选择合适的云服务器配置? 全球[VPS测评]
- 缓解云计算人才焦渴,苏州工业园区用三年引得源头活水来 全球[VPS测评]
- 新华全媒+丨第六届全球跨境电商大会:跨境电商加快拓宽中国贸易新通道 全球[VPS测评]
- Mysql连接数过高,如何检查? 全球[VPS测评]
- EdgeNAT,香港CN2+BGP vps 带宽5M 月付72元 香港VPS[主机]
- 麻花云:香港CN2VPS月付19元起,安徽移动8核/16G/20M独服2 香港VPS[主机]
- 企鹅小屋:香港ntt vps,大带宽直连,1核/1G/2T流量/半年15 香港VPS[主机]
- SugarHosts糖果主机双十一大促销 虚拟主机最高6折优惠 全球[VPS测评]
- 海量科技:香港2核2G云服务器,5M独享带宽,低至168元/月 香港VPS[主机]
- 麻花云双12活动:香港CN2云主机首月9元;安徽移动vps月付29元 香港VPS[主机]
- OneTechCloud易科云双十二优惠:香港/美国CN2全场VPS季付 美国VPS[主机]
- 者也vps年终特惠:全场云服务器7折终身优惠,赠1G内存,充1000送3 全球[VPS测评]
- 古德云:香港CN2 GIA云服务器,1核1G 2Mbps带宽仅15/月, 香港VPS[主机]
- 百纵科技:美国洛杉矶云主机4核8G大带宽仅799元/月;圣何塞vps,1 美国VPS[主机]
- 月神科技:香港沙田CN2 GIA VPS,1核/1G/10M/40GSS 香港VPS[主机]
- CoNoov:美国/日本/新加坡/英国/荷兰/西班牙/挪威/丹麦vps, 日本VPS[主机]
- windows 2012 R2系统更新KB2919355补丁,提示此更新 WINDOWS
- 香港的云服务器值得租用吗?有什么好处? 香港VPS[主机]
- Weebly网站迁移到WordPress的实操上手。 全球[VPS测评]
- 什么是linux面板?常见的Linux控制面板有哪些? 全球[VPS测评]
- “休闲养蜂”风靡海外跨境电商追逐垂直风口 全球[VPS测评]
- 快速云:科普云服务器服务器的概念和作用2022-08-2313:26来源 全球[VPS测评]
- MoonVM,香港HKT家宽VPS服务器,动态IP一键切换,1G带宽任意 香港VPS[主机]
- 金斗云怎么样?香港CN2月付23元,圣何塞CN2月付22元 香港VPS[主机]
- 企鹅小屋:香港ntt vps,大带宽直连,1核/1G/2T流量/半年15 香港VPS[主机]
- 青果云怎么样?美国/香港/日本云主机 买多久送多久 日本VPS[主机]
- tudcloud:全场7折,香港不限流量VPS,Windows系统,Li WINDOWS
转载请注明原文地址: http://140.238.13.167:12355/read-208707.html











