Php checks whether the url page is compressed using gzip.

Source: Internet
Author: User
Tags explode php code trim nginx server

What if gzip compression is enabled?

Apache

Step 2

The code is as follows: Copy code

LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so

Add the following code in http. conf:

The code is as follows: Copy code

# BEGIN GZIP
# Before enabling gzip compression for apache, install the gzip module.
<Ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
Text/plain text/xml text/css application/x-javascript
Application/javascript
</Ifmodule>
# END GZIP

If you are an nginx server, refer to the following code:

The code is as follows: Copy code

# Enable gzip compression in nginx and put it in location
Gzip on;
Gzip_min_length 1000;
Gzip_buffers 4 8 k;
Gzip_http_version 1.1;
Gzip_types text/html text/plain text/xml
Text/css application/x-javascript application/javascript;

Restart apache or nginx. How can I check whether gzip is enabled correctly? The following php code can be used.

 

The code is as follows: Copy code

// Mill military network uses gzip to compress webpages
// The webpage directly obtained by file_get_contents is garbled.
Header ('content-Type: text/html; charset = utf-8 ');
$ Url = 'http: // www.111cn.net ';
$ File = fopen ($ url, "rb ");
// Read-only 2 bytes. If the value is (in hexadecimal format) 1f 8b (in hexadecimal format) 31 139, gzip is enabled;
$ Bin = fread ($ file, 2 );
Fclose ($ file );
$ StrInfo = @ unpack ("C2chars", $ bin );
$ TypeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2']);
$ IsGzip = 0;
Switch ($ typeCode)
{
Case 31139:
// The website has enabled gzip.
$ IsGzip = 1;
Break;
Default:
$ IsGzip = 0;
}  
$ Url = $ isGzip? "Compress. zlib: //". $ url: $ url; // ternary expression
$ MierHtml = file_get_contents ($ url); // Obtain Mill military network data
$ MierHtml = iconv ("gbk", "UTF-8", $ mierHtml );
Echo $ mierHtml;

Example 2

The code is as follows: Copy code

<? Php
/*
Php determines whether the url page is compressed using gzip
*/
$ Ch = curl_init ("http://www.111cn.net/"); // url cannot have redirection
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('Accept-Encoding: gzip, deflate '));
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ Buffer = curl_exec ($ ch );
$ Curl_info = curl_getinfo ($ ch );
Curl_close ($ ch );
$ Header_size = $ curl_info ["header_size"];
// Header length
$ Headers = substr ($ buffer, 0, $ header_size );
// Obtain the header information
$ Body = substr ($ buffer, $ header_size );
// Obtain the webpage content
 
Function getEncoding (& $ headers ){
$ Arr = explode ("rn", trim ($ headers ));
Array_shift ($ arr );
Foreach ($ arr as $ header ){
List ($ k, $ v) = explode (':', $ header );
If ('content-encoding' = strtolower ($ k )){
Return trim ($ v );
        }
    }
Return false;
}   
 
$ Encoding = getEncoding ($ headers );
 
If ($ encoding ){
Echo "Using:". $ encoding;
} Else {
Echo "None ";
}
 
?>
 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.