我使用html_dom来搜索网站。
$url = $_POST["textfield"];
$html = file_get_html($url);
html_dom.php
function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
{
// We DO force the tags to be terminated.
$dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
// For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.
$contents = file_get_contents($url, $use_include_path, $context);
// Paperg - use our own mechanism for getting the contents as we want to control the timeout.
//$contents = retrieve_url_contents($url);
if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)
{
return false;
}
// The second parameter can force the selectors to all be lowercase.
$dom->load($contents, $lowercase, $stripRN);
return $dom;
}
问题是,如果互联网连接太慢,它仍然会用于file_get_html,那么它将是一个警告错误,表示无法打开流和致命错误:最长执行时间为30秒。我试图通过在检测到警告错误时停止该功能来解决它:
function errHandle($errNo, $errStr, $errFile, $errLine) {
$msg = "Slow Internet Connection";
if ($errNo == E_NOTICE || $errNo == E_WARNING) {
throw new ErrorException($msg, $errNo);
} else {
echo $msg;
}
}
set_error_handler('errHandle');
但它仍会在执行时打印致命错误。关于如何解决这个问题的任何想法?
答案 0 :(得分:0)
如果需要很长时间,您可以增加时间限制:
http://php.net/manual/en/function.set-time-limit.php
你无法在php 5.6或更低版本中捕获致命信息。在php 7+中你可以用
try {
doSomething();
} catch (\Throwable $exception) {
// error handling
echo $exception->getMessage();
}
不确定是否可以捕获执行时间限制。