phpweb-scrapingdomerror-handlingdomdocument

PHP DOMDocument error handling


I'm having trouble trying to write an if statement for the DOM that will check if $html is blank. However, whenever the HTML page does end up blank, it just removes everything that would be below DOM (including what I had to check if it was blank).

$html = file_get_contents("http://example.com/");
$dom = new DOMDocument;
@$dom->loadHTML($html);
$links = $dom->getElementById('dividhere')->getElementsByTagName('img');
foreach ($links as $link)
{
    echo $link->getAttribute('src');
}

All this does is grab an image URL in the specified div, which works perfectly until the page is a blank HTML page.

I've tried using SimpleHTMLDOM, which didn't work either (it didn't even fetch the image on working pages). Did I happen to miss something with this one or am I just missing something in both?

include_once('simple_html_dom.php')
$html = file_get_html("http://example.com/");
foreach($html->find('div[id="dividhere"]') as $div)
{
    if(empty($div->src))
    {
        continue;
    }
    echo $div->src;
}

Solution

  • Get rid on the $html variable and just load the file into $dom by doing @$dom->loadHTMLFile("http://example.com/");, then have an if statement below that to check if $dom is empty.