【问题标题】:How do I fix max execution time errors in PHP?如何修复 PHP 中的最大执行时间错误?
【发布时间】:2009-11-10 05:26:19
【问题描述】:

我有一个 PHP 页面,我通过 CRON 作业每分钟运行一次。

我已经运行了很长一段时间,但它突然开始抛出这些错误:

Maximum execution time of 30 seconds exceeded in /home2/sharingi/public_html/scrape/functions.php on line 84

每个错误的行号都会有所不同,从第 70 行到第 90 行。

这是第 0-95 行的代码

function crawl_page( $base_url, $target_url, $userAgent, $links)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_URL,$target_url);
    curl_setopt($ch, CURLOPT_FAILONERROR, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 100);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops

    $html = curl_exec($ch);

    if (!$html) 
    {
        echo "<br />cURL error number:" .curl_errno($ch);
        echo "<br />cURL error:" . curl_error($ch);
        //exit;
    }

    //
    // load scrapped data into the DOM
    //

    $dom = new DOMDocument();
    @$dom->loadHTML($html);

    //
    // get only LINKS from the DOM with XPath
    //

    $xpath = new DOMXPath($dom);
    $hrefs = $xpath->evaluate("/html/body//a");

    //
    // go through all the links and store to db or whatever
    //  

    for ($i = 0; $i < $hrefs->length; $i++) 
    {
        $href = $hrefs->item($i);
        $url = $href->getAttribute('href');

        //if the $url does not contain the web site base address: http://www.thesite.com/ then add it onto the front

        $clean_link = clean_url( $base_url, $url, $target_url);
        $clean_link = str_replace( "http://" , "" , $clean_link);
        $clean_link = str_replace( "//" , "/" , $clean_link);

        $links[] = $clean_link;

        //removes empty array values

        foreach($links as $key => $value) 
        { 
            if($value == "") 
            { 
                unset($links[$key]); 
            } 
        } 
        $links = array_values($links); 

        //removes javascript lines

        foreach ($links as $key => $value)
        {
            if ( strpos( $value , "javascript:") !== FALSE )
            {
                unset($links[$key]);
            }
        }
        $links = array_values($links);

        // removes @ lines (email)

        foreach ($links as $key => $value)
        {
            if ( strpos( $value , "@") !== FALSE || strpos( $value, 'mailto:') !== FALSE)
            {
                unset($links[$key]);
            }
        }
        $links = array_values($links);
    }   

    return $links; 
}

是什么导致了这些错误,我该如何预防?

【问题讨论】:

    标签: php execution-time


    【解决方案1】:

    您应该使用set_time_limit 函数设置max_execution 时间。如果您想要无限时间(很可能是您的情况),请使用:

    set_time_limit(0);
    

    【讨论】:

    • 安全模式下也可以在php.ini文件中修改时间限制。
    • 我不能在我的 php.ini 中修改 max_execution_time 高于 30 秒 set_time_limit(0);超越这个?
    • 如果您未处于安全模式:是的。
    • 如果这与每分钟运行的 cron 作业相关联,并且我将最大执行设置为无限,这不会导致一些问题吗?
    • 如果您的脚本没有正确完成,例如如果您的脚本运行 10 分钟,您将一直有 10 个实例,这是一个问题吗?
    【解决方案2】:

    原因:部分功能需要超过 30 秒才能完成。
    解决方案:在php配置文件中增加最大执行时间(ma​​x_execution_time)。
    1. 如果你可以访问你的全局 php.ini 文件(通常在 /web/conf 或者你可以从 phpinfo 中的配置文件 (php.ini) 路径中获取位置),将 max_execution_time=30 更改为 ma​​x_execution_time=300 .
    2. 如果您只能访问本地 php.ini 文件(您可以从 phpinfo 中的 Loaded Configuration File 中获取位置),请将 max_execution_time=30 更改为 max_execution_time=300。注意:这个文件被命名为 php5.ini 用于 php 5.x+ 和 php.ini 用于 4.x。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-10
      • 2019-08-03
      • 2018-04-18
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      相关资源
      最近更新 更多