我使用一個庫中的URL稱爲簡單的HTML DOM錯誤處理加載
一個它的方法,中加載的網址爲DOM對象:
function load_file()
{
$args = func_get_args();
$this->load(call_user_func_array('file_get_contents', $args), true);
// Throw an error if we can't properly load the dom.
if (($error=error_get_last())!==null) {
$this->clear();
return false;
}
}
爲了測試錯誤處理,我創建此代碼:
include_once 'simple_html_dom.php';
function getSimpleHtmlDomLoaded($url)
{
$html = false;
$count = 0;
while ($html === false && ($count < 10)) {
$html = new simple_html_dom();
$html->load_file($url);
if ($html === false) {
echo "Error loading url!\n";
sleep(5);
$count++;
}
}
return $html;
}
$url = "inexistent.html";
getSimpleHtmlDomLoaded($url);
這段代碼背後的想法是要再試一次,如果輸入的網址無法加載,如果經過10個attemps還是失敗,它應返回false。
但是,似乎沒有一個url,load_file方法永遠不會返回false。
相反,我得到以下警告消息:
PHP的警告:的file_get_contents(inexisten.html):未能打開流
任何想法如何解決這一問題?
注意:最好我想避免入侵圖書館。
警告的問題是什麼?是否存在警告或者您無法檢查加載文檔時是否出現問題?另外(只是一個提示)PHP有一個DomDocument,比「簡單的HTML DOM」庫好得多。請參見[如何使用PHP分析和處理HTML?](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) – hakre