stackers
stackers

Reputation: 3270

Handle errors from external library (simple_html_dom)

I'm building a page to crawl some web pages.

It works, usually, but everyone once in a while, it will fail to grab the page, and throw the following error:

( ! ) Warning: file_get_contents(URLWASHERE): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in Z:\Wamp\www\spider\simple_html_dom.php on line 555

Here is how I'm grabbing the page:

$page->load_file('URLWASHERE');

Is there a way to figure out if that error happens? I don't know how to detect it because it's in the library, not my code.

I can't use if (!$page) because it still returns something. But that something doesn't seem very helpful, though it is significantly shorter.


You can see output here:

$page when successful: http://pastebin.com/CnRVP6SK

$page when failed: http://pastebin.com/t9q6Gwnf


I just want to be able to find out if there was an error so I can have my program try again.

Upvotes: 0

Views: 118

Answers (2)

Drahcir
Drahcir

Reputation: 11972

You can use the error_get_last() function to get info about the last error. You might also consider silencing the warning message with the @ operator.

@file_get_contents('http://example.com/wjqlshqwd');

$error = error_get_last();
if($error && strpos($error['message'], '404') !== false)
{
    echo 'There was an error';
}

Also before running this code you should reset the state of error_get_last(), a comment on the PHP manual page describes a trick to do that:

// var_dump or anything else, as this will never be called because of the 0
set_error_handler('var_dump', 0);
@$undef_var;
restore_error_handler();

// error_get_last() is now in a well known state:
// Undefined variable: undef_var

The concept is just to create a known error.

Upvotes: 1

stackers
stackers

Reputation: 3270

It seems I can use

if(error_get_last())

to check if an error has been thrown so far.

This will break if other errors are encountered, but my code seems to be free of errors aside from this occasional one, so I will use this.

unfortunately, this will only allow me to try twice, rather than keep trying until it works.

Upvotes: 0

Related Questions