代码之家  ›  专栏  ›  技术社区  ›  ian

如何修复PHP中的最大执行时间错误?

  •  4
  • ian  · 技术社区  · 16 年前

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

    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; 
    }
    

    2 回复  |  直到 12 年前
        1
  •  5
  •   TRiG    12 年前

    set_time_limit 功能。如果你想要无限时间(很可能是你的情况),请使用:

    set_time_limit(0);
    
        2
  •  0
  •   Rahul    13 年前

    原因 :有些功能需要30秒以上才能完成。
    解决方案:增加最大执行时间( max_execution_time )在php配置文件中。
    1.如果您可以访问全局php.ini文件(通常在/web/conf,否则您可以从phpinfo中的配置文件(php.ini)路径获取位置),请将max_execution_time=30更改为 max_execution_time=300。
    适用于php5.x+,php.ini适用于4.x。

    推荐文章