Search code examples
phphtmlweb-scrapingfatal-errorquerypath

How to ignore a PHP catchable fatal error from QueryPath?


I'm scraping some data from a website using QueryPath. However, every so often I receive the error message below and the script terminates.

PHP Catchable fatal error:  Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given, called in ....inc/QueryPath/QueryPath/CSS/DOMTraverser.php on line 417 and defined in ....inc/QueryPath/QueryPath/CSS/DOMTraverser.php on line 467

The error doesn't give me any clues as to which line of my code the error is coming from, but assuming it was coming from $outHtml = htmlqp($outHtml); I tried prefixing the htmlqp command with @htmlqp.

This didn't work, so I then tried wrapping htmlqp in a catch{} statement which didn't seem to help either.

All I want to do is ignore the error and continue rather than having the script bomb out. Help!


Solution

  • It's a catchable fatal error .. so catch it.

    If you catch it you can get a full stacktrace.

    Ex:

    try {
       thisfunctionthrowsanexception();
    } catch (Exception $e) {
        var_dump(get_class($e));
        echo $e->getTraceAsString();
    }
    

    @ hides errors. You don't ever want to have to use that.