【问题标题】:CURLOPT_TIMEOUT not working at all (php)CURLOPT_TIMEOUT 根本不起作用(php)
【发布时间】:2014-11-02 07:52:19
【问题描述】:

我正在使用 curl 并正确设置所有参数(据我所知),但 CURLOPT_TIMEOUT 被忽略并允许无限循环。这是我的 Curl 请求的配置:

$user_agent = 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(

        CURLOPT_CUSTOMREQUEST => "GET", //set request type post or get

        CURLOPT_POST => false, //set to GET

        CURLOPT_USERAGENT => $user_agent, //set user agent

        CURLOPT_COOKIEFILE => dirname(__FILE__)."/cookie.txt", //set cookie file

        CURLOPT_COOKIEJAR => dirname(__FILE__)."/cookie.txt", //set cookie jar

        CURLOPT_RETURNTRANSFER => true, // return web page

        CURLOPT_SSL_VERIFYPEER, FALSE, //ignore ssl

        CURLOPT_PROXY =>$proxy['ip'],

        CURLOPT_PROXYPORT =>$proxy['port'],

        CURLOPT_HEADER => false, // don't return headers

        CURLOPT_FOLLOWLOCATION => true, // follow redirects

        CURLOPT_ENCODING => "", // handle all encodings

        CURLOPT_AUTOREFERER => true, // set referrer on redirect

        CURLOPT_CONNECTTIMEOUT => 20, // timeout on connect

        CURLOPT_TIMEOUT => 10, // timeout on response

        CURLOPT_MAXREDIRS => 10, // stop after 10 redirects

    );
    $ch = curl_init($url);

    curl_setopt_array($ch, $options);

    $content = curl_exec($ch);

    $err = curl_errno($ch);

    $errmsg = curl_error($ch);

    $header = curl_getinfo($ch);
    curl_close($ch);

我不是最擅长调试的,所以我不确定问题可能是什么。请帮帮我。

【问题讨论】:

  • 您使用的是哪个版本的 CUrl。如果版本 >= 7.16.2,请尝试使用 CURLOPT_CONNECTTIMEOUT_MS。您可以使用<?php phpinfo(); ?>查看版本
  • 我尝试使用 CURLOPT_CONNECTTIMEOUT_MS 但我仍然遇到无限循环
  • 可能不相关,但在您的代码中是这样的:CURLOPT_SSL_VERIFYPEER, FALSE, //ignore ssl - 故意的?
  • 因为如果是这样它应该是 CURLOPT_SSL_VERIFYPEER => FALSE, //ignore ssl
  • @user3284327 也可以在将CURLOPT_FOLLOWLOCATION 设置为 false 后尝试

标签: php curl timeout infinite-loop


【解决方案1】:

curl_setopt($connection, CURLOPT_TIMEOUT, $seconds) 应该在 curl_exec() 函数之前调用。

当我早点调用它时它对我不起作用

【讨论】:

  • 有没有相关的文档?这对我来说似乎不对...除非您没有注意到的其他代码正在更改选项,否则我希望 any CURLOPT_* 值将设置在 @ 返回的资源对象上987654322@,坚持。
猜你喜欢
  • 2011-08-22
  • 1970-01-01
  • 2018-06-16
  • 2015-03-02
  • 2012-07-31
  • 1970-01-01
  • 2017-09-08
  • 2011-07-01
  • 2011-10-26
相关资源
最近更新 更多