Open smallearth opened 8 years ago
Hi,
it's a bit late, I know, but if you see at load_file()
it's basically a wrap to file_get_contents()
that allows to set a stream context in which you can define a timeout. The timeout will emit a warning (formally E_WARNING). So, to keep it clean you can define an error handler and use a try/catch to execute the code. An example:
// catch all
set_error_handler(function($severity, $message, $filename, $lineno) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}, -1 & ~E_NOTICE & ~E_USER_NOTICE);
$url = ''; // define the url
$html = new simple_html_dom();
try {
$options = ['http' => ['method' => 'GET', 'timeout' => 10]]; // 10 seconds
$context = stream_context_create($options);
$result = $html->load_file($url, NULL, $context);
} catch (ErrorException $e) {
$result = 'There was an error'; // change to empty string
}
print $result . PHP_EOL;
print 'done' . PHP_EOL;
The arguments passed to load_file()
are those of the file_get_contents()
function, which signature looks like this:
string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = 0 [, int $maxlen ]]]] )
function load_file How to deal with overtime issues?