2014-11-14 117 views
0

我有一個問題,我一直在試圖解決過去幾天。 我有一個網站,我抓取新聞,並完美的作品。 但是,最近我遇到了我的analyzer_script問題,因爲它似乎超過了我的虛擬主機設置的時間限制。顯然,大約1分鐘有一個max_execution時間,而且我的腳本比這更長。因爲我在公共服務器上託管我的網站,所以我無法在php.ini腳本中調整它。 我能做什麼?我需要重寫我的腳本嗎?max_execution時間限制php

我感謝您的幫助! 我的腳本如下:

<?php 
    $array = array(); 
    $sub_array = array(); 
    $analyzer_ids = array(); 

$res5 = mysqli_query($con,"SELECT id,status FROM statuz ORDER BY id DESC LIMIT 1"); 
$row5 = mysqli_fetch_array($res5); 

$status = $row5['status']; 
$status_id = $row5['id']; 

if($status == 2) { 

    $res1 = mysqli_query($con,"SELECT tag, id FROM tags"); 
    while($row1 = mysqli_fetch_array($res1)) { 
     $tag = $row1['tag']; 
     $id = $row1['id']; 

     $res2 = mysqli_query($con,"SELECT sub_tag FROM sub_tags WHERE tag_id = '$id'"); 
     while($row2 = mysqli_fetch_array($res2)) { 
      $sub_tag = $row2['sub_tag']; 
      $sub_tag = strtolower($sub_tag); 
      $sub_array[] = $sub_tag; 
     } 

     $array[] = array('tag_id' => $id, 'tag' => $tag, 'sub_tag' => $sub_array); 
     $sub_array = array(); 
    } 

    mysqli_query($con,"INSERT INTO analyzer_queue (crawler_id, status) 
    (SELECT id,0 FROM crawlers)"); 

    $initial_res = mysqli_query($con,"SELECT crawler_id,id FROM analyzer_queue WHERE status = '0'"); 
    while($initial_row = mysqli_fetch_array($initial_res)) { 

     $analyzer_id = $initial_row['id']; 
     $start_crawler_id = $initial_row['crawler_id']; 
     mysqli_query($con,"UPDATE analyzer_queue SET status = '1' WHERE crawler_id = '$start_crawler_id' ORDER BY id DESC LIMIT 1"); 
     $analyzer_ids[] = $analyzer_id; 

       $res = mysqli_query($con,"SELECT cr.title, cr.content, cr.id 
       FROM crawler_results cr 
       INNER JOIN crawlers c 
       ON c.newspaper_id = cr.newspaper_id 
       WHERE c.id = '$start_crawler_id' 
       AND status = '3' 
       LIMIT 10"); 
       while($row = mysqli_fetch_array($res)) { 

        $article_id = $row['id']; 
        $title = $row['title']; 
        $content = $row['content']; 

        $content = strip_tags($content); 
        $content = strtolower($content); 
        $title = strtolower($title); 


        $count = array(); 
        foreach ($array as $tag) { 
         $regex = '/(?:\b' . preg_quote($tag['tag'], '/'); 
         foreach ($tag['sub_tag'] as $sub) { 
          $regex .= '\b)|(?:\b' . preg_quote($sub, '/'); 
         } 
         $regex .= '\b)/i'; 
         $count_content = preg_match_all($regex, $content, $count_content); 
         $count_title = preg_match_all($regex, $title, $count_title); 
         $count_total[$tag['tag']] = $count_content + $count_title; 
         $total_count = $count_total[$tag['tag']]; 

         $tag_name = $tag['tag']; 

         $res5 = mysqli_query($con,"SELECT id FROM tags WHERE tag = '$tag_name'"); 
         $row5 = mysqli_fetch_array($res5); 

         $tag_id = $row5['id']; 


         if($total_count != 0) { 
          mysqli_query($con,"INSERT INTO article_tags (article_id,tag_id,count_tags) VALUES('$article_id','$tag_id','$total_count')"); 
         } 

         echo$count_total[$tag['tag']]; 
         echo"<br /><br />"; 
        } 

        echo"<pre>"; 
        print_r($count_total); 
        echo"</pre>"; 

        mysqli_query($con,"UPDATE crawler_results SET status = '2', analyzer_id = '$analyzer_id' WHERE id = '$article_id'"); 
       } 
       mysqli_query($con,"UPDATE analyzer_queue SET status = '2' WHERE crawler_id = '$start_crawler_id' ORDER BY id DESC LIMIT 1"); 

     } 
     mysqli_query($con,"UPDATE crawler_results SET status = '4' WHERE analyzer_id NOT IN (".implode(',',$analyzer_ids).")"); 
     mysqli_query($con,"UPDATE statuz SET status = '3' WHERE id = '$status_id'"); 
     print_r($analyzer_ids); 

} else { 
    echo"Not ready yet"; 
} 




?> 
+0

當使用'mysqli'你應該使用參數化查詢,而['bind_param'](http://php.net/manual/en/mysqli- stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值來實現此目的,因爲您將創建嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 – tadman 2014-11-14 21:02:27

回答

0

試試這個:

ini_set('max_execution_time', 0); 

The original Post

+0

完美KrustyBox!這是非常快的!我接受誰的答案? oliakaoil提供了更多的細節,但你快了3分鐘。 – 2014-11-14 21:41:24

+0

我的代碼就是你需要的。我比@oliakaoil少點... :) – KrustyBox 2014-11-15 01:53:49

1

你可以在你的腳本是這樣的頂部使用ini_set

ini_set('max_execution_time' , 300); 

以上調用將腳本的最大執行時間爲5分鐘。這裏的文檔頁面:

http://php.net/manual/en/function.ini-set.php

如果你的主機允許的話,您也可以考慮通過運行cron或其他一些調度守護這個腳本。運行在cli上下文中的PHP腳本通常沒有執行時間限制,或者更高的執行時間限制。

+0

非常感謝你回答我的問題,快速oliakaoil!不幸的是KrustyBox比你快3分鐘,但你在答案中提供了更多細節。我該怎麼辦? – 2014-11-14 21:40:14

+0

不客氣。我在回答「我該做什麼」問題時最不客觀:-),所以我會棄權。 – oliakaoil 2014-11-14 21:55:08