This article describes the method of using curl to obtain header detection to enable GZip compression. Share it for your reference, as follows:
Obtaining web header information is a common technology used by website developers and maintenance personnel. The header information on the web page is very rich, and it is generally difficult for non-professionals to understand and understand the meaning of each project.
There are many ways to obtain web header information. As for the php language, as a rookie, I know so many ways. Here are the following one by one.
Method 1: Useget_headers()
function
This method is used by many people and is very simple and convenient. It only requires two lines of code to complete it. as follows:
$thisurl = "https:///"; print_r(get_headers($thisurl, 1));
The results obtained are:
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] =>
[Date] => Wed, 15 Aug 2018 01:31:48 GMT
[Connection] => close
[Content-Length] => 89251
)
Method 2: Usehttp_response_header
The code is also very simple, only three lines:
$thisurl = "https:///"; $html = file_get_contents($thisurl ); print_r($http_response_header);
The results obtained are:
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:
[6] => Date: Wed, 15 Aug 2018 01:34:15 GMT
[7] => Connection: close
[8] => Content-Length: 89282
)
Method 3: Usestream_get_meta_data()
function
The code also has only three lines:
$thisurl = "https:///"; $fp = fopen($thisurl, 'r'); print_r(stream_get_meta_data($fp));
The results obtained are:
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:
[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:///
[timed_out] =>
[blocked] => 1
[eof] =>
)
The above three methods can easily obtain web header information, and the information contained is already quite rich, meeting general requirements. Unfortunately, none of the above three methods cannot be used to detect whether the web page has GZip compression enabled. To detect GZip compression, other methods are required. What is introduced here iscurl()
function to detect.
Getting header with curl can detect GZip compression
Post the code first:
<?php $szUrl = '/'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $szUrl); curl_setopt($curl, CURLOPT_HEADER, 1); //Output header informationcurl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Do not display web page contentcurl_setopt($curl, CURLOPT_ENCODING, ''); //Allow gzip execution$data=curl_exec($curl); if(!curl_errno($curl)) { $info = curl_getinfo($curl); $httpHeaderSize = $info['header_size']; //header string volume $pHeader = substr($data, 0, $httpHeaderSize); //Get header string $split = array("\r\n", "\n", "\r"); //The header string needs to be formatted $pHeader = str_replace($split, '<br>', $pHeader); //Format the output to the web page using <br>newline character echo $pHeader; } ?>
The output result 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: /
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:
Date: Fri, 19 Jul 2013 09:27:21 GMT
In the above output, you can see an item: Content-Encoding: gzip, which is the item we use to determine whether the web page is compressed.
In addition, you need to pay careful attention to the comments in this example. No one can be missing, otherwise the header information may be obtained incorrectly.
For more information about PHP related content, please check out the topic of this site:Summary of the usage of php curl》、《Summary of PHP network programming skills》、《Complete collection of PHP array (Array) operation techniques》、《Summary of usage of php strings》、《PHP data structure and algorithm tutorial"and"Summary of data operation techniques for json format in PHP》
I hope this article will be helpful to everyone's PHP programming.