Php: how to read a remote gzip compressed webpage; php: how to read gzip compressed _ PHP Tutorial

Source: Internet
Author: User
Php reads the remote gzip compressed webpage and php reads the gzip compressed webpage. Php reads the remote gzip compressed webpage. When php reads the gzip compressed webpage information of a mall today, it uses file_get_contents or curl: The copy code is as follows: $ u php reads the remote gzip compression webpage, and php reads the gzip compression

When retrieving the page information of a mall today, use file_get_contents or curl:


The code is as follows:


$ Url = 'http: // www.xxx.com/21 /? Type = 23 ′;
$ Temp = file_get_contents ($ url );
Echo $ temp;


All of the results are garbled. I checked a lot of content, including the header information of the page, and found that the original page was used.

Similar information, that is, the Content-Encoding is gzip, that is, the site has enabled gzip compression. There are many solutions here. of course, if you use file_get_contents, you can modify it as follows:

The code is as follows:


File_get_contents ("compress. zlib: //". $ url );

Or use curl to complete:


The code is as follows:


Function curl_get ($ url, $ gzip = false ){
$ Curl = curl_init ($ url );
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curl, CURLOPT_CONNECTTIMEOUT, 10 );
If ($ gzip) curl_setopt ($ curl, CURLOPT_ENCODING, "gzip"); // The key here
$ Content = curl_exec ($ curl );
Curl_close ($ curl );
Return $ content;
}

At the same time, refer to the above solution in the manual, you can also save the gzip string for processing as follows:

The code is as follows:


Function gzdecode ($ data ){
$ G = tempnam ('/tmp', 'FF ');
@ File_put_contents ($ g, $ data );
Ob_start ();
Readgzfile ($ g );
$ D = ob_get_clean ();
Return $ d;
}

When retrieve the page information of a mall today, use file_get_contents or curl: the code is as follows: $ u...

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.