2012-02-14 87 views
2

我有一個名爲GrabUrTime的應用程序,它是一個時間表查看工具,從另一個網站,我的大學的網站空間獲取它的時間表。每個凌晨2點我運行一個腳本,使用解析器將所有時間表擦除並將其轉儲到我的數據庫中。PHP簡單的HTML DOM解析器:讓它循環,直到沒有錯誤

但今天uni的服務器運行不好,我的腳本不斷給我uni的服務器上的錯誤500,使腳本無法繼續運行。這是週期性的,並非總是如此。不過,我嘗試了幾次,它只是隨機發生,根本沒有任何模式。

因此,我想讓我的腳本來處理錯誤,並使其循環,直到它獲取數據。

function grabtable($intakecode, $week) { 
$html = file_get_html("http://webspace.apiit.edu.my/schedule/intakeview_intake.jsp?Intake1=".$intakecode."&Week=" . $week); 
$dumb = $html->find('table[border=1] tr'); 
$thatarray = array(); 
     for ($i=1; $i < sizeof($dumb);++$i){ 
     $arow = $html->find('table[border=1] tr', $i); 
     $date = $arow->find('td font', 0)->innertext; 
     $time = $arow->find('td font', 1)->innertext; 
     $room = $arow->find('td font', 2)->innertext; 
     $loca = $arow->find('td font', 3)->innertext; 
     $modu = $arow->find('td font', 4)->innertext; 
     $lect = $arow->find('td font', 5)->innertext; 
     $anarray = array($date, $time, $room, $loca, $modu, $lect); 
     $thatarray[$i] = $anarray; 

     //echo "arraylol"; 
    } 
    //echo serialize($tablearray)."<br/>"; 
    $html->clear(); 
    return $thatarray; 
} 

回答

2

嘗試這樣:

function getHttpCode($url) 
{ 
    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; 
    $ch=curl_init(); 
    curl_setopt ($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch,CURLOPT_VERBOSE,false); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
    $page=curl_exec($ch); 

    //echo curl_error($ch); 
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 

    if($httpcode>=200 && $httpcode<300) 
    { 
     // YOUR CODE 
    } 
else 
{ 
    // What you want to do should it fail 
    // perhaps this will serve you better as while loop, e.g. 
    // while($httpcode>=200 && $httpcode<300) { ... } 
} 

使用

getHttpCode($url); 

它可能不是整齊地插入到你的代碼,因爲它是,但我敢肯定它可以與幫助很少重新考慮因素以適應您現有的代碼結構。

+0

因此,我必須重寫整個代碼,因爲我使用HTML DOM解析器:P – 2012-02-14 09:23:39

+0

@AnonozChong,我不這麼認爲,你能不能像沿着傳遞'grabtable'函數參數將成爲httpcode。然後在函數本身檢查httpcode是否正確,如果不正確,重新運行該函數或類似的東西? – martincarlin87 2012-02-14 09:30:35

+0

哦......我錯過了手冊中的某些東西,對不起,我會嘗試你的代碼。任何想法如何使它成爲一個無限循環,直到我得到的HTML? – 2012-02-14 09:35:27