Weird error using PHP Simple HTML DOM parser

Viewed 27403

I am using this library (PHP Simple HTML DOM parser) to parse a link, here's the code:

function getSemanticRelevantKeywords($keyword){
    $results = array();
    $html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=");
    foreach($html->find('span') as $e){
            $results[] = $e->plaintext;
    }
    return $results;
}

but I am getting this error when I output the results:

Fatal error: Call to a member function find() on a non-object in /var/www/vhosts/efamous.de/subdomains/sandbox/httpdocs/getNewTrusts.php on line 25

(line 25 is the foreach loop), the odd thing is that it outputs everything (at least seemingly) correctly but I still get that error and can't figure out why.

9 Answers

Before file_get_html/load_file method, you should first check if URL exists or not.

If the URL exists, you pass one step.
(Some servers, service a 404 page a valid HTML page. which has propriate HTML page structure like body, head, etc. But it has only text "This page couldn'!t find. 404 error bla bla..)

If URL is 200-OK, then you should check whether fetched thing is object and whether nodes are set.

That's the code i used in my pages.

function url_exists($url){
    if ((strpos($url, "http")) === false) $url = "http://" . $url;
    $headers = @get_headers($url);
    // print_r($headers);
    if (is_array($headers)){
        if(strpos($headers[0], '404 Not Found'))
            return false;
        else
            return true;    
    }         
    else
        return false;
}

$pageAddress='http://www.google.com';
if ( url_exists($pageAddress) ) {
    $htmlPage->load_file( $pageAddress );
} else {
    echo 'url doesn t exist, i stop';
    return;
}

if( $htmlPage && is_object($htmlPage) && isset($htmlPage->nodes) )
{
    // do your work here...
} else {
    echo 'fetched page is not ok, i stop';
    return;
}

I'm having the same error come up in my logs and apart from the solutions mentioned above, it could also be that there is no 'span' in the document. I get the same error when searching for divs with a particular class that doesn't exist on the page, but when searching for something that I know exists on the page, the error doesn't pop up.

Related