【问题标题】:Maximum execution time exceeded in CURLCURL 超过最大执行时间
【发布时间】:2016-05-07 11:38:58
【问题描述】:

我确实有这个源代码:

LIST_CUSTOMERS.PHP

<head>
<script src="http://code.jquery.com/jquery-2.2.0.min.js"></script>
</head>

<select name='customers' id='customers'>
    <option value='john'>John</option>
    <option value='james'>James</option>
</select>

<script>
$('select').on('change', function()
{
    var username = $('#customers').find(":selected").text();

    $.post("display.php", {user : username}, function(result)
    {
        $("#content").html(result);
    });
});
</script>

<div id="content" style="width: 100%; height: 800px;">
</div>

DISPLAY.PHP

<?php
include "function.php";

if(isset($_POST["user"]))
{
    echo webLogin($_POST["user"], "1ex!AM?plE2");
}
?>

FUNCTION.PHP

<?php
function get_string_between($string, $start, $end)
{
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

function curlRequest($url,$data)
{
    $fp = fopen("cookie.txt", "w");
    fclose($fp);
    $login = curl_init();
    curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($login, CURLOPT_TIMEOUT, 40000);
    curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($login, CURLOPT_URL, $url);
    curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($login, CURLOPT_POST, TRUE);
    curl_setopt($login, CURLOPT_POSTFIELDS, $data);
    ob_start();
    return curl_exec ($login);
    ob_end_clean();
    curl_close ($login);
    unset($login);    
}

function webLogin($user, $pass)
{
    // LoginURL
    $loginUrl = 'http://webmail.example.com/login.php';

    // Formfields Login
    $form_fields_1 = array(
    'user' => $user,
    'pass' => $pass
    );

    // Formfields Mails
    $form_fields_2 = array(
    'message_id' => 0,
    );

    // Log into website
    $content = curlRequest($loginUrl, $form_fields_1);

    // Get the number of messages
    if(strpos($content, "0 Mails.")==false)
    {
        // Got mails! -> Get exact number of emails
        $buffer = get_string_between($buffer,"<div id='number_of_mails'>","</div>");

        // Delete old content
        $content = "";

        // Create link to read mail
        $link = "http://webmail.example.com/mailbox.php";

        // Display all emails
        for($i=0; $i<=(int)$buffer-1; $i++)
        {
            $form_fields_2['message_id'] = $i + 1;

            // Login ... AGAIN ...
            $fp = fopen("cookie.txt", "w");
            fclose($fp);
            $login = curl_init();
            curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
            curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
            curl_setopt($login, CURLOPT_TIMEOUT, 40000);
            curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($login, CURLOPT_URL, $loginUrl);
            curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($login, CURLOPT_POST, TRUE);
            curl_setopt($login, CURLOPT_POSTFIELDS, $form_fields_1);

            // Get email with id $i+1
            curl_setopt($login, CURLOPT_URL, $link);
            curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($login, CURLOPT_POST, TRUE);
            curl_setopt($login, CURLOPT_POSTFIELDS, $form_fields_3);
            ob_start();
            // Write results in $mail_content
            $mail_content = curl_exec ($login);
            ob_end_clean();
            curl_close ($login);
            unset($login);

            // Compose output
            $content.="Message " . $form_fields_2['message_id'] . ": <br/>";
            $content.=get_string_between($mail_content,"<div class=\"messages\">","</div>");
            $content.="</br><br/>";
        }
    }
    else
    {
        // Got no mails!
        $content = "Keine Mails!";
    }

    return $content;
}
?>

这基本上是登录到 http://webmail.example.com,获取一些电子邮件并为我显示它们。我在 XAMPP 的本地计算机上运行它,并从我的远程邮件服务器(Horde Webmail)请求一些东西。一切正常。

问题是,在 30 分钟内执行了大约 50-60 次后,我突然收到一条消息:“超过 30 秒的最大执行时间”。我当然知道我可以只更改我的 XAMPP 中的参数并且它会起作用,但这不是我想要做的,因为它会变慢。有谁知道为什么在几次 CURL-Request 之后它变得如此缓慢,我该如何防止呢?

【问题讨论】:

  • 你为什么认为它与卷曲有关?您是否检查了远程服务器上的日志/响应时间?
  • 我应该检查什么特别的东西?我查看了日志,没有什么特别的。
  • weblmail.example.com 服务器端的时间?

标签: php curl xampp


【解决方案1】:

这可能是远程服务器上的连接限制...

它可能基于用户代理,很难知道...... 试试这个:

curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

值得一试。

祝你好运

【讨论】:

  • 谢谢,我改了,但没用。
猜你喜欢
  • 1970-01-01
  • 2020-04-29
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 2018-08-01
相关资源
最近更新 更多