为什么此错误处理函数会导致domdocument()挂起? [英] why would does this error handling function cause domdocument() to hang?

查看:100
本文介绍了为什么此错误处理函数会导致domdocument()挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包括此简单的错误处理功能来格式化错误:

I include this simple error handling function to format errors:

date_default_timezone_set('America/New_York');

// Create the error handler.
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {

    // Build the error message.
    $message = "An error occurred in script '$e_file' on line $e_line: \n<br />$e_message\n<br />";

    // Add the date and time.
    $message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";

    // Append $e_vars to the $message.
    $message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />";

    echo '<div id="Error">' . $message . '</div><br />';

} // End of my_error_handler() definition.

// Use my error handler.
set_error_handler ('my_error_handler');

当我在脚本中包含以下内容时

When I include it in a script in with the following

$dom = new DOMDocument();
$dom->loadHTML($output);
$xpath = new DOMXPath($dom);

并解析网页(在这种情况下, http://www.ssense.com/women/designers/all/all/page_1 ,我确实有权解析)

and parse a web page (in this case, http://www.ssense.com/women/designers/all/all/page_1, which I do have permission to parse) I get errors like

AN ERROR OCCURRED IN SCRIPT '/HSPHERE/LOCAL/HOME/SITE.COM/SCRIPT.PHP' ON LINE 59: 
DOMDOCUMENT::LOADHTML(): HTMLPARSEENTITYREF: NO NAME IN ENTITY, LINE: 57

AN ERROR OCCURRED IN SCRIPT '/HSPHERE/LOCAL/HOME/SITE.COM/SCRIPT.PHP' ON LINE 59: 
DOMDOCUMENT::LOADHTML(): TAG NAV INVALID IN ENTITY, LINE: 58

有很多错误,页面无法完全加载。但是,如果我不包括该错误处理程序,则该行

There are many errors and the page never finishes loading. However, if I do not include this error handler, the line

$dom->loadHTML($output);

不会抛出任何错误,并且我会在几秒钟内得到预期的结果。我假设错误处理程序正在捕获与loadHTML()有关的警告,否则未报告这些警告。 (即使我使用

does not throw any errors, and I get the results I expect in a few seconds. I assume the error handler is catching warnings related to loadHTML() that are not otherwise reported. (Even if I use

@$dom->loadHTML($output);

它仍然报告错误。)如何修改错误处理程序以适应对loadHTML()的调用,否则可以解决此问题? p>

it still reports the errors.) How might I modify the error handler to accommodate calls to loadHTML(), or otherwise fix this problem?

推荐答案

不是自定义错误处理程序导致了错误。

It's not the custom error handler that is causing the error.

我在没有自定义错误处理程序的情况下运行了以下代码:

I ran the following code without a custom error handler:

$output = file_get_contents("http://www.ssense.com/women/designers/all/all/page_1");
$dom = new DOMDocument();
$dom->loadHTML($output);
$xpath = new DOMXPath($dom);

当我运行它时,我收到大量警告消息,类似于您的错误处理程序中的警告消息。

When I ran it, I got a ton of warning messages similar to the ones in your error handler.

我认为您看到的问题仅仅是您的错误处理程序正在报告PHP默认情况下未报告的错误。

I think the problem you're seeing is just that your error handler is reporting errors that PHP isn't reporting by default.

默认情况下,错误报告的级别由您的 php.ini 设置确定,但可以使用 error_reporting覆盖()函数。设置自己的错误处理程序时,必须自己确定要处理的报告级别。您的错误处理程序将在 every 错误和通知时被调用,因此您将输出 everything 的错误消息,除非您针对当前<$ c $明确检查生成的错误c> error_reporting()级。

By default, the level of error reporting is determined by your php.ini settings, but can be overridden by using the error_reporting() function. When you set your own error handler, you have to determine for yourself what level of reporting you want to deal with. Your error handler will be called on every error and notice, and so you will output error messages for everything unless you explicitly check the error being generated against the current error_reporting() level.

请记住,使用 @ 错误抑制运算符只是为该行设置 error_reporting(0)的简写。例如,以下行:

Remember that using the @ error suppression operator is just shorthand for setting error_reporting(0) for that line. For example, this line:

@$dom->loadHTML($output);

是以下各项的简写:

$errorLevel = error_reporting(0);
$dom->loadHTML($output);
error_reporting($errorLevel);

由于使用自定义处理程序时完全绕过了正常的PHP错误报告,因此使用 @ 运算符毫无意义,因为当前 error_reporting()级别已被完全忽略。您必须将自定义代码写入错误处理程序,以检查当前的 error_reporting()级别并进行相应的处理,例如:

Since normal PHP error reporting is entirely bypassed when using a custom handler, using the @ operator is meaningless since the current error_reporting() level is completely ignored. You would have to write custom code into your error handler to check the current error_reporting() level and handle it accordingly, for example:

function my_error_handler() {
  if (error_reporting() == 0) {
    return; // do nothing when error_reporting is disabled.
  }

  // normal error handling here
}

我的假设是,当不使用自定义错误处理程序时,PHP只是默认为 error_reporting()级别,该级别低于产生的错误。

My assumption is that, when not using a custom error handler, PHP is simply defaulting to an error_reporting() level which is lower than the errors being produced.

如果在代码顶部添加 error_reporting(E_ALL | E_STRICT); ,您甚至还会看到相同的错误当您未启用自定义错误处理程序时。

If you add error_reporting(E_ALL | E_STRICT); to the top of your code, you will see those same errors even when you don't have your custom error handler enabled.

这篇关于为什么此错误处理函数会导致domdocument()挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆