PHP DOMDocument Error Handling Problem

Posted by Jon on Stack Overflow See other posts from Stack Overflow or by Jon
Published on 2010-12-31T02:50:25Z Indexed on 2010/12/31 2:54 UTC
Read the original article Hit count: 281

Filed under:
|
|
|
|

I'm having trouble trying to write an if statement for 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;
}

© Stack Overflow or respective owner

Related posts about php

Related posts about dom