CURL中超出了最大执行时间

时间:2016-01-29 16:38:02

标签: php curl xampp

我确实有这个源代码:

  

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)请求一些东西。一切都很好。

问题是在执行此约50-60次并且30分钟之后我突然收到消息:“超过30秒的最大执行时间”。我当然知道我可以在我的XAMPP中更改参数并且它可以工作,但这不是我想做的,因为它的速度慢。有谁知道为什么在几次CURL-Request后它变得如此缓慢,以及我怎么能阻止它呢?

1 个答案:

答案 0 :(得分:0)

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

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

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

值得一试。

祝你好运