有时候在验证网站时,需要采集网页标题(title)、描述(description)、关键字(keywords)等信息,本文将其整理出来,总结的经验分享给大家。
<?phpheader("content-type:text/html; charset=utf8");//网页编码 gb2312 utf8$arr = file("http://www.dedecmsok.com");if($arr){ foreach($arr as $a){ if(strchr($a,"<title>")){ $a = str_ireplace("<title>","",$a); $a = str_ireplace("</title>","",$a); echo $a;//标题 break; } }}$meta_array = get_meta_tags('http://www.niaorenit.com');echo $meta_array["keywords"];//关键词echo $meta_array["description"];//描述?>
注意:如果是https的网址,PHP.ini 默认配置下,用 file_get_contents 读取 https 的链接,就会如下错误:
Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?
这个错误三种解决方案:
1. windows 下的 PHP,只需要到 php.ini 中把 extension=php_openssl.dll 前面的 ; 删掉,重启服务就可以了。
2. linux 下的 PHP,就必须安装 openssl 模块,安装好了以后就可以访问了。
3. 如果服务器你不能修改配置的话,那么就使用 curl 函数来替代 file_get_contents 函数,当然不是简单的替换,还有相应的参数配置才能正常使用 curl 函数。
本文章部分图片素材及文字内容等均来源于网络,仅供学习参考,如有侵犯您的版权,请联系我们,本站核实后将尽快删除或改正。