tags:

views:

44

answers:

1

I have a php page that I run ever minute through a CRON job.

I have been running it for quite some time but suddenly it started throwing up these errors:

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

The line # will vary with each error ranging from line 70 up into the 90s

Here is the code from lines 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; 
}
+2  A: 

you should set the max_execution time using set_time_limit function

if you want infinite time (most likelu your case)

set_time_limit(0);
RageZ
the time limit can also be modified in the php.ini file if running in safe mode.
Eric
I cant modify the max_execution_time higher than 30 seconds in my php.ini does set_time_limit(0); over ride this?
ian
@Eric thanks for the comment
RageZ
if you are not in safe mode: yes.
RageZ
If this is tied to a cron job that runs every minute and I set the max execute to infinite couldn't that cause some problems?
ian
if you script finish properly not, for example if you script run 10 minute you would have 10 instance around all the time is that an issue ?
RageZ
Doubt it but what if I end up with a lot more than that? I got it at 120 seconds right now and it's fine. Maybe I will just give it a high threshold.
ian
maybe you should make some kind of instance count and if there is too much instance around stop executing it ..
RageZ