Home >Backend Development >PHP Tutorial >How to read remote gzip compressed web pages in php, read gzip compression in php_PHP tutorial

How to read remote gzip compressed web pages in php, read gzip compression in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:361066browse

How to read remote gzip compressed web pages in php, read gzip compression in php

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


Copy code The code is as follows:

$url = 'http://www.xxx.com/21/?type=23′;
$temp = file_get_contents($url);
echo $temp;


They all got garbled code. After checking a lot of content, including the header information of the page, I found that the original page used it.

Similar information, that is, Content-Encoding is gzip, which means that the site has gzip compression turned on. There are many solutions here. Of course, if you use file_get_contents, you can modify it like this:

Copy code The code is as follows:

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

Or use curl to do it:


Copy code 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 is here
           $content = curl_exec($curl);                                                        curl_close($curl);                                                           return $content;                                           }





At the same time, refer to the solution above in the manual, you can also process strings saved in gzip type as follows:

Copy code

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;
}






http://www.bkjia.com/PHPjc/934936.html
www.bkjia.com

true

TechArticleHow to read remote gzip compressed web pages in php, read gzip compression in php and retrieve the page information of a mall today When using file_get_contents or curl: Copy the code as follows: $u...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn