登录  /  注册

php如何只抓取网页头

藏色散人
发布: 2021-10-14 09:16:20
原创
1694人浏览过

php只抓取网页头的方法:1、使用get_headers()函数;2、使用http_response_header方法;3、使用stream_get_meta_data()函数;4、使用php curl来获取网页头即可。

php如何只抓取网页头

本文操作环境: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 = &#39;http://www.lao8.org/&#39;;
$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, &#39;&#39;); //允许执行gzip
$data=curl_exec($curl); 
if(!curl_errno($curl))
{
    $info = curl_getinfo($curl);
    $httpHeaderSize = $info[&#39;header_size&#39;];  //header字符串体积
    $pHeader = substr($data, 0, $httpHeaderSize); //获得header字符串
    $split   = array("rn", "n", "r");  //需要格式化header字符串
    $pHeader = str_replace($split, &#39;<br>&#39;, $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中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号