This article describes how to solve the problem of using file_get_contents to capture Chinese garbled characters in PHP. You can use curl to configure the gzip option for reference.
This article describes how to solve the problem of using file_get_contents to capture Chinese garbled characters in PHP. You can use curl to configure the gzip option for reference.
This example describes how to solve the problem of using file_get_contents to capture Chinese garbled characters on webpages in PHP. Share it with you for your reference. The specific method is as follows:
The file_get_contents function is a very good built-in local and remote file operation function in php. It allows us to download remote data directly without any lingering efforts, however, when I use it to read a webpage, some pages may be garbled. Here I will summarize the specific solutions for you.
According to a friend on the Internet, the reason may be that GZIP compression is enabled on the server. The following describes how to use firebug to check the header information of my website. Gzip is enabled and the original header information of the request header is returned, the Code is as follows:
The Code is 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 = canonical; _ utmz = 225240837.1326850415.887.3.utmcsr = google | utmccn = (organic) | utmcmd = organic | utmctr = % E4 % 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% 20 site % 3A; PHPSESSID = 888mj4jwp8s0m7s0frre3ovc7; _ utmc = 225240837; _ utmb = 225240837.1.10.1335411401
Host
User-Agent Mozilla/5.0 (Windows NT 5.1; rv: 12.0) Gecko/20100101 Firefox/12.0
You can find from the header information that the Content-Encoding item is Gzip.
The solution is relatively simple, that is, use curl instead of file_get_contents to get it, and then add a line in the curl configuration parameter. The Code is as follows:
The Code is as follows:
Curl_setopt ($ ch, CURLOPT_ENCODING, "gzip ");
When I captured the image with file_get_contents today, I didn't find this problem. I just found it out after I spent a lot of effort.
Use the built-in zlib library. If the server has installed the zlib library, use the following code to easily solve the garbled problem. The Code is as follows:
The Code is as follows:
$ Data = file_get_contents ("compress. zlib: //". $ url );
I hope this article will help you with PHP programming.
,