Home  >  Article  >  Backend Development  >  Solve the problem of Chinese garbled web pages captured by file_get_contents in PHP_PHP Tutorial

Solve the problem of Chinese garbled web pages captured by file_get_contents in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:38868browse

According to friends on the Internet, the reason may be that GZIP compression is enabled on the server.

The following is the header information of my blog using firebug. Gzip is turned on.

Request header information original header information

Copy the code as follows
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept- Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Connection keep-alive
Cookie __utma=225240837.787252530.1317310581.1335406161.1335411401.1537; __utmz=2 25240837.1326850415.887.3.utmcsr =google|utmccn=(organic)|utmcmd=organic|utmctr=%E4%BB%BB%E4%BD%95%E9%A1%B9%E7%9B%AE%E9%83%BD%E4%B8% 8D%E4%BC%9A%E9%82%A3%E4%B9%88%E7%AE%80%E5%8D%95%20site%3Awww.nowamagic.net; PHPSESSID=888mj4425p8s0m7s0frre3ovc7; __utmc=225240837; __utmb= 225240837.1.10.1335411401
Host www.111cn.net
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0


You can find that the Content-Encoding item is Gzip from the header information.

The solution is relatively simple.

Just use curl instead of file_get_contents to get it, and then add a

to the curl configuration parameters

The code is as follows Copy the code
curl_setopt($ch, CURLOPT_ENCODING, "gzip");

When I used file_get_contents to capture pictures today, I didn’t notice this problem at first, and it took a lot of effort to find it out

1. Use the built-in zlib library

If the server has installed the zlib library, you can easily solve the garbled code problem by using the following code.

The code is as follows Copy the code
$data = file_get_contents("compress.zlib://".$url);

Of course, you can also use the curl module to solve my problem. Here I recommend an article to you, the following article is of interest to you

For more details, please check: http://www.111cn.net/phper/31/58895.htm

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/742586.htmlTechArticleAccording to friends on the Internet, the reason may be that the server has turned on GZIP compression. The following is the header information of my blog viewed using firebug. Gzip is turned on. Request header information original header information code...
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