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
http: //www.bkjia.com/PHPjc/934936.htmlTechArticleHow 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...