将此代码与 simplehtmldom 脚本一起使用(http://simplehtmldom.sourceforge.net/manual.htm):
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;
}
$url = 'http://site.com/';
$html = file_get_html($url);
如何处理错误file_get_html($url)
部分?现在,如果页面不存在,它会在浏览器窗口中显示错误。我更喜欢抓住它们并显示我的文字,例如:
if(some error happened on file_get_html($url)) {
$errors = true;
} else {
html = file_get_html($url);
}
Thanks.
你好
您需要检查 404 Not Found 消息,因为无论如何都会返回一个数组。
function url_exists($url){
if ((strpos($url, "http")) === false) $url = "http://" . $url;
$headers = @get_headers($url);
//print_r($headers);
if (is_array($headers)){
//Check for http error here....should add checks for other errors too...
if(strpos($headers[0], '404 Not Found'))
return false;
else
return true;
}
else
return false;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)