在将html文件加载到DOMDocument时,是否可以尝试捕获PHP警告?
是否可以执行某种类型的try-catch来捕获警告 e、 g 对于在将html文件加载到DOMDocument时,是否可以尝试捕获PHP警告?,php,error-handling,try-catch,warnings,domdocument,Php,Error Handling,Try Catch,Warnings,Domdocument,是否可以执行某种类型的try-catch来捕获警告 e、 g 对于$url我正在使用的我正在获取 警告(2):DOMDocument::loadHTMLFile(MYURL)[DOMDocument.loadHTMLFile]:无法打开流:HTTP请求失败!HTTP/1.0 403禁止 [APP\controllers\import\u controller.php,第62行] 警告(2):DOMDocument::loadHTMLFile()[DOMDocument.loadHTMLFile]
$url
我正在使用的我正在获取
警告(2):DOMDocument::loadHTMLFile(MYURL)[DOMDocument.loadHTMLFile]:无法打开流:HTTP请求失败!HTTP/1.0 403禁止
[APP\controllers\import\u controller.php,第62行]
警告(2):DOMDocument::loadHTMLFile()[DOMDocument.loadHTMLFile]:I/O警告:无法加载外部实体“hMYURL”[APP\controllers\import\u controller.php,第62行]
我可以用@
抑制错误,如果调用返回false
,我可以做一些事情,但我希望能够捕捉到确切的警告消息,然后对其进行处理。
这可能吗?您可能可以使用,但我不能完全确定您收到的警告是否可以处理。某些错误或警告将始终使用默认的错误处理程序。但是试试看。你可能正在寻找这个
<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// function to test the error handling
function scale_by_log($vect, $scale)
{
if (!is_numeric($scale) || $scale <= 0) {
trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
}
if (!is_array($vect)) {
trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
return null;
}
$temp = array();
foreach($vect as $pos => $value) {
if (!is_numeric($value)) {
trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
$value = 0;
}
$temp[$pos] = log($scale) * $value;
}
return $temp;
}
// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");
// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
print_r($a);
// now generate second array
echo "----\nvector b - a notice (b = log(PI) * a)\n";
/* Value at position $pos is not a number, using 0 (zero) */
$b = scale_by_log($a, M_PI);
print_r($b);
// this is trouble, we pass a string instead of an array
echo "----\nvector c - a warning\n";
/* Incorrect input vector, array of values expected */
$c = scale_by_log("not array", 2.3);
var_dump($c); // NULL
// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
$d = scale_by_log($a, -2.5);
var_dump($d); // Never reached
?>
您应该为此使用
本示例改编自手册:
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$res = $doc->loadHTMLFile($url); //this may fail and return FALSE!
foreach (libxml_get_errors() as $error) {
// handle errors here
}
libxml_clear_errors();
这里不会发布PHP通知。我刚刚在以下网站找到了一个非常有吸引力的解决方案:
首先告诉PHP跟踪所有错误:
ini_set('track_errors', '1');
这会将所有错误消息(包括警告)放入变量$php\u errormsg
(请参阅)
然后您可以捕获如下错误和警告:
if (!@copy('no_file', 'some_file')) {
echo $php_errormsg, "\n";
}
可能重复的
ini_set('track_errors', '1');
if (!@copy('no_file', 'some_file')) {
echo $php_errormsg, "\n";
}