在 CURL 中超出了最大执行时间


Maximum execution time exceeded in CURL

我确实有这个源代码:

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>

显示。.PHP

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

功能。.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 中的本地计算机上运行它,并从我的远程邮件服务器(部落网络邮件)请求一些东西。一切都很好。

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

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

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

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');

值得一试。

祝你好运