php只抓取网页头的方法:1、使用get_headers()函数;2、使用http_response_header方法;3、使用stream_get_meta_data()函数;4、使用php CURL来获取网页头即可。
本文操作环境:windows7系统、PHP7.1版、DELL G3电脑
php如何只抓取网页头?
php获取网页header信息的4种方法
php获取网页header信息的方法多种多样,就php语言来说,我知道的方法有4种, 下面逐一献上。
方法一:使用get_headers()函数
推荐指数: ★★★★★
get_header方法最简单只要两行代码即可搞定。如下:
$thisurl = "http://www.lao8.org/"; print_r(get_headers($thisurl, 1));
得到的结果为:
Array ( [0] => HTTP/1.1 200 OK [Cache-Control] => max-age=86400 [Content-Length] => 76102 [Content-Type] => text/html [Content-Location] => http://www.lao8.org/index.html [Last-Modified] => Fri, 19 Jul 2013 03:52:30 GMT [Accept-Ranges] => bytes [ETag] => "50bc48643384ce1:5cb3" [Server] => Microsoft-IIS/6.0 [X-Powered-By] => ASP.NET [Date] => Fri, 19 Jul 2013 09:06:39 GMT [Connection] => close )
方法二:使用http_response_header
推荐指数: ★★★
http_response_headerf方法也很简单,仅三行:
$thisurl = "http://www.lao8.org"; $html = file_get_contents($thisurl ); print_r($http_response_header);
得到的结果为:
Array ( [0] => HTTP/1.1 200 OK [1] => Cache-Control: max-age=86400 [2] => Content-Length: 76102 [3] => Content-Type: text/html [4] => Content-Location: http://www.lao8.org/index.html [5] => Last-Modified: Fri, 19 Jul 2013 03:52:30 GMT [6] => Accept-Ranges: bytes [7] => ETag: "50bc48643384ce1:5cb3" [8] => Server: Microsoft-IIS/6.0 [9] => X-Powered-By: ASP.NET [10] => Date: Fri, 19 Jul 2013 09:06:41 GMT [11] => Connection: close )
方法三:使用stream_get_meta_data()函数
推荐指数: ★★★
使用stream_get_meta_data()代码也只需三行:
$thisurl = "http://www.lao8.org/"; $fp = fopen($thisurl, 'r'); print_r(stream_get_meta_data($fp));
得到的结果为:
Array ( [wrapper_data] => Array ( [0] => HTTP/1.1 200 OK [1] => Cache-Control: max-age=86400 [2] => Content-Length: 76102 [3] => Content-Type: text/html [4] => Content-Location: http://www.lao8.org/index.html [5] => Last-Modified: Fri, 19 Jul 2013 03:52:30 GMT [6] => Accept-Ranges: bytes [7] => ETag: "50bc48643384ce1:5cb3" [8] => Server: Microsoft-IIS/6.0 [9] => X-Powered-By: ASP.NET [10] => Date: Fri, 19 Jul 2013 09:06:41 GMT [11] => Connection: close ) [wrapper_type] => http [stream_type] => tcp_socket [mode] => r+ [unread_bytes] => 1086 [seekable] => [uri] => http://www.lao8.org/ [timed_out] => [blocked] => 1 [eof] => )
第四种方法: 使用php的高级函数 CURL()来获取
推荐指数: ★★★★
上面的三种方法能获取一般的网页header信息,如果想要获取更详细的header信息比如网页是否启用了GZip压缩。这时候可以用php的高级函数curl()来获取。
使用curl获得header可以检测GZip压缩
先贴出代码:
<?php $szUrl = 'http://www.lao8.org/'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $szUrl); curl_setopt($curl, CURLOPT_HEADER, 1); //输出header信息 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //不显示网页内容 curl_setopt($curl, CURLOPT_ENCODING, ''); //允许执行gzip $data=curl_exec($curl); if(!curl_errno($curl)) { $info = curl_getinfo($curl); $httpHeaderSize = $info['header_size']; //header字符串体积 $pHeader = substr($data, 0, $httpHeaderSize); //获得header字符串 $split = array("rn", "n", "r"); //需要格式化header字符串 $pHeader = str_replace($split, '<br>', $pHeader); //使用<br>换行符格式化输出到网页上 echo $pHeader; } ?>
输出结果如下:
HTTP/1.1 200 OK Cache-Control: max-age=86400 Content-Length: 15189 Content-Type: text/html Content-Encoding: gzip Content-Location: http://www.lao8.org/index.html Last-Modified: Fri, 19 Jul 2013 03:52:28 GMT Accept-Ranges: bytes ETag: "0268633384ce1:5cb3" Vary: Accept-Encoding Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET Date: Fri, 19 Jul 2013 09:27:21 GMT
可以看到使用curl获取到的header信息多了这行:Content-Encoding: gzip,网页启用了GZip压缩。
推荐学习:《PHP视频教程》
以上是php如何只抓取网页头的详细内容。更多信息请关注PHP中文网其他相关文章!

本文比较了酸和基本数据库模型,详细介绍了它们的特征和适当的用例。酸优先确定数据完整性和一致性,适合财务和电子商务应用程序,而基础则侧重于可用性和

本文讨论了确保PHP文件上传的确保,以防止诸如代码注入之类的漏洞。它专注于文件类型验证,安全存储和错误处理以增强应用程序安全性。

本文讨论了在PHP中实施API速率限制的策略,包括诸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之类的库。它还涵盖监视,动态调整速率限制和手

本文讨论了使用password_hash和pyspasswify在PHP中使用密码的好处。主要论点是,这些功能通过自动盐,强大的哈希算法和SECH来增强密码保护

本文讨论了OWASP在PHP和缓解策略中的十大漏洞。关键问题包括注射,验证损坏和XSS,并提供用于监视和保护PHP应用程序的推荐工具。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器