2012-02-14 89 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