【发布时间】:2010-08-07 16:29:55
【问题描述】:
我正在使用具有此功能的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 and 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 行在上面的代码中。
我怎样才能正确处理这个错误,我可以只使用一个普通的ifcondition,它看起来不像返回一个布尔值。
感谢大家的帮助
更新
这是一个好的解决方案吗?
if(fopen(urlencode(trim("$next_url")), 'r')){
$html3 = file_get_html(urlencode(trim("$next_url")));
}else{
//do other stuff, error_logging
return false;
}
【问题讨论】:
-
为什么是通过url而不是文件名调用的?
-
这是个好问题,所以 +1
-
@Abs 这可能是因为您无缘无故地对所有答案投了反对票。
-
@Abs(和 Col):如何使用 @ 和返回代码比捕获异常更不合法的“错误处理”,或者您目前在“这是一个好的解决方案吗? '。就因为@?
标签: php error-handling