这是一个主意:
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:请参见手册



