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
- 疯狂猜成语 图猜成语一个黑色的人拎着一个写着道字的袋子还有一个感叹号是什全球[VPS测评]
- HostXen 香港四区上线 CN2 线路 自由 DIY 配置香港VPS[主机]
- GinerNet→€19.99 月 4GB 内存 50GB NVMe 空虚拟空间(主机)
- $4 月 512M 内存 20G SSD 1Gbps 不限流量 KVM 日本VPS[主机]
- 数脉科技劳动节促销,香港 BGP 直连服务器,10M 无限流量,E3 E香港VPS[主机]
- 特价 Dedipath→洛杉矶服务器 不限流量 全场 8 折优惠 1Gb全球[VPS测评]
- 通过给WordPress文章添加3篇假的页码导航实现相关文章全球[VPS测评]
- 如何在begin主题实现评论自动排第一全球[VPS测评]
- 盘点2021年国内外VPS云服务器可视化面板和一键安装脚本全球[VPS测评]
- RivenCloud 1G内存 10G SSD 5M带宽不限流量 全区x全球[VPS测评]
- 阿里云(中国区) 香港新加坡区轻量应用服务器香港VPS[主机]
- on9host 1核心 512M内存 15G 储存 500G流量 30M全球[VPS测评]
- Vultr新用户100美金 最新活动来啦!新用户充值活动来了,充100美全球[VPS测评]
- 企鹅小屋怎么样?深港IPLC专线销售 500M大带宽/2核1G内存/55全球[VPS测评]
- ExCloud:1核256M存/8GB SSD空间/100GB流量/OV虚拟空间(主机)
- 云服务器和服务器各有什么优缺点?全球[VPS测评]
- 20个温暖心灵的问题,抖音,朋友圈都可以拿去发。全球[VPS测评]
- 傲闪云vps,1核1G低至10元起,国内高防/香港CN2/美国CN2/美美国VPS[主机]
- CubeCloud,靠谱的香港cn2vps,美国cn2终身九折,原生IP美国VPS[主机]
- 孤狼云:香港沙田CN2/安畅CN2云服务器仅9.9元/月_香港虚拟主机2香港VPS[主机]
- 亿速云11.11上云狂欢节活动:2核4G国内/香港云服务器,价格999元香港VPS[主机]
- 恒创科技:开年采购季_香港CN2云服务器/美国CN2线路,低至299元/美国VPS[主机]
- 腾讯云香港轻量应用服务器 优惠活动最低月付仅24元香港VPS[主机]
- 极光KVM:新上香港CN2VPS,终身6折优惠,1核/1G/5Mbps,香港VPS[主机]
- 影速科技怎么样?影速科技海外云服务器价格多少钱?全球[VPS测评]
- 啤啤云:美国圣何塞vps/香港安畅vps,GIA线路,全场9折终身优惠美国VPS[主机]
- 详细介绍vps云服务器及其作用全球[VPS测评]
- Vmshell:香港vps调整,流量提升至500GB/月,带宽200MB香港VPS[主机]
- 柚子互联:美国洛杉矶CN2 vps,防御100G/带宽10M,28元/月美国VPS[主机]
- 企鹅小屋:香港VPS限时3折优惠,香港沙田CN2,原生IP,年付486.香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-214631.html











