php uses curl to obtain header detection to turn on GZip compression method

  • 2021-10-27 06:47:35
  • OfStack

In this paper, an example is given to describe the method of php using curl to obtain header detection and turn on GZip compression. Share it for your reference, as follows:

Obtaining header information of web pages is a common technology for website developers and maintenance personnel. The header information of the web page is very rich, and it is difficult for non-professionals to read and understand the meaning of each project.

There are many ways to get header information. As far as php language is concerned, as a rookie, I know four ways. Here are one by one.

Method 1: Use get_headers() Function

This method is used by many people, and it is also very simple and convenient. It only needs two lines of code to get it done. As follows:


$thisurl = "https://www.ofstack.com/";
print_r(get_headers($thisurl, 1));

The results are as follows:

Array
(
[0] = > HTTP/1.1 200 OK
[Content-Type] = > text/html
[Last-Modified] = > Wed, 15 Aug 2018 01:23:03 GMT
[ETag] = > "99a921833634d41:0"
[Server] = > Microsoft-IIS/7.5
[X-Powered-By] = > ofstack.com
[Date] = > Wed, 15 Aug 2018 01:31:48 GMT
[Connection] = > close
[Content-Length] = > 89251
)

Method 2: Use http_response_header

The code is also very simple, only three lines:


$thisurl = "https://www.ofstack.com/";
$html = file_get_contents($thisurl ); 
print_r($http_response_header);

The results are as follows:

Array
(
[0] = > HTTP/1.1 200 OK
[1] = > Content-Type: text/html
[2] = > Last-Modified: Wed, 15 Aug 2018 01:33:04 GMT
[3] = > ETag: "7b9757e93734d41:0"
[4] = > Server: Microsoft-IIS/7.5
[5] = > X-Powered-By: ofstack.com
[6] = > Date: Wed, 15 Aug 2018 01:34:15 GMT
[7] = > Connection: close
[8] = > Content-Length: 89282
)

Method 3: Use stream_get_meta_data() Function

There are only three lines of code:


$thisurl = "https://www.ofstack.com/";
$fp = fopen($thisurl, 'r'); 
print_r(stream_get_meta_data($fp));

The results are as follows:

Array
(
[wrapper_data] = > Array
(
[0] = > HTTP/1.1 200 OK
[1] = > Content-Type: text/html
[2] = > Last-Modified: Wed, 15 Aug 2018 01:38:45 GMT
[3] = > ETag: "ecc8f8b43834d41:0"
[4] = > Server: Microsoft-IIS/7.5
[5] = > X-Powered-By: ofstack.com
[6] = > Date: Wed, 15 Aug 2018 01:39:35 GMT
[7] = > Connection: close
[8] = > Content-Length: 89421
)
[wrapper_type] = > http
[stream_type] = > tcp_socket/ssl
[mode] = > r
[unread_bytes] = > 7945
[seekable] = >
[uri] = > https://www.ofstack.com/
[timed_out] = >
[blocked] = > 1
[eof] = >
)

All the above three methods can easily obtain header information of web pages, and the information contained is quite rich, meeting the requirements of 1. Unfortunately, none of the above three methods can be used to detect whether GZip compression is enabled on web pages. To detect GZip compression, other methods are needed. What is introduced here is to use curl() Function to detect.

GZip compression can be detected by obtaining header using curl

Post the code first:


<?php
$szUrl = 'http://www.webkaka.com/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $szUrl);
curl_setopt($curl, CURLOPT_HEADER, 1); // Output header Information 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Do not display web page content 
curl_setopt($curl, CURLOPT_ENCODING, ''); // Allow execution gzip
$data=curl_exec($curl); 
if(!curl_errno($curl))
{
  $info = curl_getinfo($curl);
  $httpHeaderSize = $info['header_size']; //header String volume 
  $pHeader = substr($data, 0, $httpHeaderSize); // Obtain header String 
  $split  = array("\r\n", "\n", "\r"); // Formatting is required header String 
  $pHeader = str_replace($split, '<br>', $pHeader); // Use <br> Newline character formatted output to web page 
  echo $pHeader;
}
?>

The output is as follows:

HTTP/1.1 200 OK
Cache-Control: max-age=86400
Content-Length: 15189
Content-Type: text/html
Content-Encoding: gzip
Content-Location: http://www.webkaka.com/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

In the above output, we can see one project: Content-Encoding: gzip, which is the project we use to judge whether GZip compression is enabled.

In addition, you need to pay careful attention to the comments in this example, and you can't miss any one item, otherwise you may get the header information incorrectly.

For more readers interested in PHP related contents, please check the topics of this site: "php curl Usage Summary", "PHP Network Programming Skills Summary", "PHP Array (Array) Operation Skills Encyclopedia", "php String (string) Usage Summary", "PHP Data Structure and Algorithm Tutorial" and "json Format Data Operation Skills Summary in PHP"

I hope this article is helpful to everyone's PHP programming.


Related articles: