simple_html_dom.php中的错误处理

时间:2011-12-20 06:29:33

标签: php

我是PHP的初学者,我遇到类似的问题:

Good error handling with file_get_contents

在simple_html_dom.php中,有一个名为load_file的函数,它是:

    function load_file() {
    $args = func_get_args();
    $this->load(call_user_func_array('file_get_contents', $args), true);
    }

在我的PHP脚本中,我将此函数用作:

$ HTML-> LOAD_FILE($链路);

当我尝试加载断开的链接时,我在输出显示屏上收到一条警告消息,如:

警告:file_get_contents(http://www.yurowdesigns.com/UkraineSIG/test.asp)[function.file-get-contents]:无法打开流:HTTP请求失败!第568行/home/yurow/wwwroot/yurowdesigns.com/programs/simple_html_dom.php中找不到HTTP / 1.1 404

我想将此类错误消息重新路由到我网站上的error.log文件,而不是将其放在输出显示器上。

我天真地试图调整

中给出的答案

Good error handling with file_get_contents

通过在我的simple_html_dom.php副本中添加函数fget_contents()来解决我的问题。

function fget_contents() {
$args = func_get_args();
// the @ can be removed if you lower error_reporting level
$contents = @call_user_func_array('file_get_contents', $args);

if ($contents === false) {
    throw new Exception('Failed to open ' . $file);
} else {
    return $contents;
}
}

并将load_file中的第568行更改为:

$ this-> load(call_user_func_array('fget_contents',$ args),true);

但是现在,当我运行我的PHP脚本时,我收到一条新的错误消息:

警告:call_user_func_array()要求参数1是有效的回调函数,找不到函数'fget_contents'或者在第568行/home/yurow/wwwroot/yurowdesigns.com/programs/simple_html_dom.php中找到无效的函数名

这意味着simple_html_dom.php无法识别函数'fget_contents'

我哪里出错了?我该如何解决?

2 个答案:

答案 0 :(得分:1)

使用以下代码:

function load_file() {
    try{
       $args = func_get_args();
       $this->load(call_user_func_array('file_get_contents', $args), true);
    } catch(Exception e) {
      print_r(e);
    }
}

在这里,你看到它有try和catch块来处理错误。

答案 1 :(得分:0)

为此,在PHP文件中,将它们设置为隐藏显示的错误。使用ini_set('display_errors','1');,然后使用error_log记录您的错误,或者希望您的默认php.ini配置已使用error_log string

记录错误