大神们好,我的程序使用如下方式 读取XML:
$xmlElement = simplexml_load_file($ntp_file);
if ($xmlElement == false) {
$this->set_error(101100, "parse_ntp_file_error!");
return false;
}
官方的说明是 simplexml_load_file失败返回false,但我这样做了,还是报了个警告的错误:抛出一些错误信息,破坏我的json输出:
parser error : Input is not proper UTF-8, indicate encoding
注意,我不是查这个错误原因,我知道这原因,我是想问大神,如何捕获这个错,自己处理掉,而不是直接报错。但判断返回值是不是false不生效。
2 回答
![?](http://img1.sycdn.imooc.com/533e4c9c0001975102200220-100-100.jpg)
POPMUISE
TA贡献1765条经验 获得超5个赞
针对 warning
,
我想到了两种方法:
-
添加错误抑制符
@
$xmlElement = @simplexml_load_file($ntp_file);
控制
warning
输出范围
通过控制php.ini
让warning
只在 日志里输出,即:将display_errors
值,改为Off
参考:PHP 运行时配置-display_errors
![?](http://img1.sycdn.imooc.com/5458464a00013eb602200220-100-100.jpg)
侃侃无极
TA贡献2051条经验 获得超10个赞
try{
$xmlElement = simplexml_load_file($ntp_file);
if ($xmlElement == false) {
$this->set_error(101100, "parse_ntp_file_error!");
return false;
}
} catch(\Exception $e) {
$this->set_error( $e->getCode(), $e->getMessage());
return false;
} catch(\Error $e) {
$this->set_error( $e->getCode(), $e->getMessage());
}
- 2 回答
- 0 关注
- 370 浏览
添加回答
举报
0/150
提交
取消