忽略PHP cURL超时 [英] PHP cURL timeout ignored

查看:157
本文介绍了忽略PHP cURL超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 curl_setopt()我已将 CURLOPT_CONNECTTIMEOUT_MS 设置为1000(1秒)另一个脚本睡眠5秒钟,然后响应200 OK(使用 sleep()),我称之为测试目的。我的脚本总是等待响应,即使它应该在cURL超时错误中产生。



如何使超时按预期工作,并中断请求? p>

  $ ch = curl_init($ url); 
curl_setopt_array($ ch,array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_NOBODY => TRUE,
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
CURLOPT_CONNECTTIMEOUT_MS => 1000,
CURLOPT_MAXREDIRS => 5,
CURLOPT_USERAGENT =>'Linkit / 2.x Drupal / 7.x',
) ;
$ document = curl_exec($ ch);

我也尝试过 CURLOPT_TIMEOUT_MS



我使用PHP 5.3.4与cURL 7.19.7在OS X上10.6,XAMPP。

解决方案

CURLOPT_CONNECTTIMEOUT CURLOPT_CONNECTTIMEOUT_MS 定义cURL连接到服务器所需的最长时间,但在您的情况下,连接成功,因此超时不再适用。



您需要使用 CURLOPT_TIMEOUT CURLOPT_TIMEOUT_MS ,它们定义最大时间cURL可以执行。



有关PHP支持的选项的完整列表,请查看 curl_setopt 文档


Using curl_setopt() I have set CURLOPT_CONNECTTIMEOUT_MS to 1000 (1 second) and have set up another script that sleeps for 5 seconds, then responds 200 OK (using sleep()) which I call for testing purposes. My script always waits for the response, even though it should yield in a cURL timeout error.

How do I make the timeout work as expected and interrupt the request?

$ch = curl_init($url);
curl_setopt_array($ch, array(
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_FOLLOWLOCATION => TRUE,
  CURLOPT_NOBODY => TRUE,
  CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
  CURLOPT_CONNECTTIMEOUT_MS => 1000,
  CURLOPT_MAXREDIRS => 5,
  CURLOPT_USERAGENT => 'Linkit/2.x Drupal/7.x',
));
$document = curl_exec($ch);

I have also tried CURLOPT_TIMEOUT_MS and also the variants without the _MS suffixes.

I'm using PHP 5.3.4 with cURL 7.19.7 on OS X 10.6, XAMPP.

解决方案

The CURLOPT_CONNECTTIMEOUT or CURLOPT_CONNECTTIMEOUT_MS define the maximum amount of time that cURL can take to connect to the server but in your case, the connection is successful so the time-out no longer applies.

You need to use CURLOPT_TIMEOUT or CURLOPT_TIMEOUT_MS which define the maximum amount of time cURL can execute for.

For a complete list of options supported by PHP, look at the curl_setopt documentation.

这篇关于忽略PHP cURL超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆