2017-01-08 17 views
0

所以我有這個,如何處理HTTP錯誤在簡單的HTML DOM解析器

$r1pro = file_get_html('http://www.example.com/forums-profile.php?user_id='.$uid); 
$uname = $r1pro->find('span.username', 0); 

它完美,但該網站是不是很WELLY設計,如果你沒有登錄,某些請求返回500錯誤,其輸出是這樣的:

Warning: file_get_contents(http://www.example.com/forums-profile.php?user_id=12345678): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /home/mysite/public_html/su3/htmldom.php on line 76 
Fatal error: Call to a member function find() on boolean in /home/mysite/public_html/su3/index.php on line 61 

我搜索過的錯誤simplehtmldom處理方法,但都一無所獲。有什麼辦法可以捕獲http錯誤並改用其他代碼嗎?

回答

0

沒關係,我覺得現在真的很愚蠢。我只需要做的就是

if($r1pro){ 
<--do normal stuff if no error--> 
}else{ 
<--do stuff in case of error--> 
}