使用file_get_contents进行良好的错误处理

时间:2010-08-07 16:29:56

标签: php error-handling

我正在使用具有此功能的simplehtmldom

// get html dom form file
function file_get_html() {
    $dom = new simple_html_dom;
    $args = func_get_args();
    $dom->load(call_user_func_array('file_get_contents', $args), true);
    return $dom;
}

我这样使用它:

$html3 = file_get_html(urlencode(trim("$link")));

有时,URL可能无效,我想处理此问题。我以为我可以使用try和catch但这没有用,因为它不会抛出异常,它只是给出一个像这样的php警告:

[06-Aug-2010 19:59:42] PHP Warning:  file_get_contents(http://new.mysite.com/ghs 1/) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found  in /home/example/public_html/other/simple_html_dom.php on line 39

第39行在上面的代码中。

如何才能正确处理此错误,我可以使用普通的if条件,看起来它不会返回布尔值。

感谢大家的帮助

更新

这是一个很好的解决方案吗?

if(fopen(urlencode(trim("$next_url")), 'r')){

    $html3 = file_get_html(urlencode(trim("$next_url")));

}else{
    //do other stuff, error_logging
    return false;

}

5 个答案:

答案 0 :(得分:14)

这是一个想法:

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;
    }
}

基本上是file_get_contents的包装器。它会在失败时抛出异常。 为避免必须覆盖file_get_contents本身,您可以

// change this
$dom->load(call_user_func_array('file_get_contents', $args), true); 
// to
$dom->load(call_user_func_array('fget_contents', $args), true); 

现在你可以:

try {
    $html3 = file_get_html(trim("$link")); 
} catch (Exception $e) {
    // handle error here
}

错误抑制(通过使用@或降低error_reporting级别是有效解决方案。这可以抛出异常,您可以使用它来处理错误。有很多原因为什么file_get_contents可能会生成警告,PHP的手册本身建议降低error_reporting:See manual

答案 1 :(得分:4)

使用CURL获取网址并以这种方式处理错误响应。

来自curl_init()的简单示例:

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

答案 2 :(得分:2)

从我的POV来看,良好的错误处理是PHP的一大挑战。幸运的是,您可以注册自己的错误处理程序并自行决定要做什么。

您可以定义一个相当简单的错误处理程序,如下所示:

function throwExceptionOnError(int $errorCode , string $errorMessage) {
    // Usually you would check if the error code is serious
    // enough (like E_WARNING or E_ERROR) to throw an exception
    throw new Exception($errorMessage);
}

并将其注册到您的函数中:

function file_get_html() {
    $dom = new simple_html_dom;
    $args = func_get_args();
    set_error_handler("throwExceptionOnError");
    $dom->load(call_user_func_array('file_get_contents', $args), true);
    restore_error_handler();
    return $dom;
}

答案 3 :(得分:1)

要查看为什么file_get_contents调用可能失败的原因,您可以使用php的error_get_last函数:

delete it
from
  ImageTable it
 join
  Temp_Image ti on it.Id=ti.Id;

insert ImageTable
select * from Temp_Image;

答案 4 :(得分:0)

如果您从外部URL获取,最好的处理方式将来自他引入像Zend_Http这样的HTTP库。这与使用CURL或fopen有很大的不同,除了它将这些“dirvers”的细节提取到通用API中,然后您可以选择要使用的内容。它也会有一些内置的错误捕获,以使你更容易。

如果你不想要另一个库的开销那么你可以自己编码 - 在这种情况下我总是喜欢CURL。

相关问题