get_meta_tags()& get_headers()? [英] Exception handling with get_meta_tags() & get_headers()?
本文介绍了get_meta_tags()& get_headers()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP中,我使用 get_meta_tags()
和 get_headers()
,但是当有404 ,这两个功能发出警告。有没有办法抓住它?
谢谢!
In PHP, I am using get_meta_tags()
and get_headers()
, however, when there is a 404, those two functions throw a warning. Is there any way for me to catch it?
Thanks!
推荐答案
get_headers
不会发出警告/ 404错误,但是 get_meta_tags
。
get_headers
does not throw a Warning/Error on 404, but get_meta_tags
does.
所以你可以检查标题响应,做一些事情,当它是不行:
So you can check the header response and do something, when it's not OK:
$url = 'http://www.example.com/';
$headers = array();
$metatags = array();
$validhost = filter_var(gethostbyname(parse_url($url,PHP_URL_HOST)), FILTER_VALIDATE_IP);
if($validhost){
// get headers only when Domain is valid
$headers = get_headers($url, 1);
if(substr($headers[0], 9, 3) == '200'){
// read Metatags only when Statuscode OK
$metatags = get_meta_tags($url);
}
}
这篇关于get_meta_tags()& get_headers()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文