Home >Backend Development >PHP Tutorial >Solution to file_get_contents in php to obtain garbled web pages_PHP tutorial
Yesterday when I was doing a simple collection function, I directly used the file_get_contents function, but there was no problem in collecting some websites, and some network chips were collected. Later, I analyzed that the garbled code appeared because the server had turned on the gzip compression function.
A page I collected, as follows gzip
It will be easier for us to solve it once we know the reason. We first searched on Baidu and found out that we can use curl operation instead.
curl solution
The code is as follows
|
Copy code
|
||||||||||||||||
function curl_get($url, $gzip=false){
Adopt gzip encoding format file_get_contents solution:
# wget http://curl.haxx.se/download/curl-7.17.1.tar.gz # tar zxvf curl-7.17.1.tar.gz //解压 #cd curl-7.17.1 # ./configure –prefix=/usr/local/curl # make # make install : Modify the settings of the php.ini file and find |