SimpleHTMLDOMというライブラリを使用しています
そのメソッドの1つは、URLを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);
このコードの背後にある考え方は、URLの読み込みに失敗した場合に再試行することです。それでも、10回試行しても失敗した場合は、falseを返す必要があります。
ただし、URLが存在しない場合、load_fileメソッドがfalseを返すことはないようです。
代わりに、次の警告メッセージが表示されます。
PHP警告:file_get_contents(inexisten.html):ストリームを開くことができませんでした
これを修正する方法はありますか?
注:できれば、ライブラリへのハッキングは避けたいと思います。