Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method that php only grabs the header of the web page?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is the method that php only grabs the web page header". In the daily operation, I believe that many people have doubts about the method of php only grabbing the web page header. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of php only grabbing the web page header?" Next, please follow the editor to study!

Php only grabs the web page header: 1, use the get_headers () function; 2, use the http_response_header method; 3, use the stream_get_meta_data () function; 4, use php CURL to get the page header.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

How does php grab only the page header?

Four methods of obtaining header Information of Web Page by php

There are many ways for php to get header information from web pages. As far as the php language is concerned, there are four ways I know. Here are some of them.

Method 1: use the get_headers () function

Recommended index: ★

The get_header method is as simple as two lines of code. As follows:

$thisurl = "http://www.lao8.org/";print_r(get_headers($thisurl, 1))

The results are as follows:

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)

Method 2: use http_response_header

Recommended index: ★★★

The http_response_headerf method is also simple, with only three lines:

$thisurl = "http://www.lao8.org";$html = file_get_contents ($thisurl); print_r ($http_response_header)

The results are as follows:

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)

Method 3: use the stream_get_meta_data () function

Recommended index: ★★★

It only takes three lines to use the stream_get_meta_data () code:

$thisurl = "http://www.lao8.org/";$fp = fopen ($thisurl,'r'); print_r (stream_get_meta_data ($fp))

The results are as follows:

Array ([wrapper_data] = > Array ([0] = > HTTP/1.1 200OK [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] = >)

The fourth method: use php's high-level function CURL () to get the

Recommended index: ★★★★

The above three methods can get general web page header information, if you want to get more detailed header information such as whether the web page has GZip compression enabled. At this point, you can use php's high-level function curl () to get it.

Use curl to get header to detect GZip compression

Post the code first:

The output is as follows:

HTTP/1.1 200 OKCache-Control: max-age=86400Content-Length: 15189Content-Type: text/htmlContent-Encoding: gzipContent-Location: http://www.lao8.org/index.htmlLast-Modified: Fri, 19 Jul 2013 03:52:28 GMTAccept-Ranges: bytesETag: "0268633384ce1:5cb3" Vary: Accept-EncodingServer: Microsoft-IIS/6.0X-Powered-By: ASP.NETDate: Fri, 19 Jul 2013 09:27:21 GMT

You can see that there is more header information obtained using curl: Content-Encoding: gzip, and the web page has GZip compression enabled.

At this point, the study on "what is the method of php only grabbing web page headers" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report