This article illustrates the use of File_get_contents Crawl Web page in PHP to solve Chinese garbled problem. Share to everyone for your reference. The specific methods are as follows:
The file_get_contents function is inherently a very good PHP native and remote file manipulation function, it can let us not spend the power to the remote data directly download, but I use it to read the page will encounter some pages are garbled, here to give you a summary of specific solutions.
According to a friend on the internet said the reason may be the server opened gzip compression, the following is used Firebug to view the head of my site information, gzip is open, request header information Raw header information, the code is as follows:
Copy Code 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=225240837.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.jb51.net
User-agent mozilla/5.0 (Windows NT 5.1; rv:12.0) gecko/20100101 firefox/12.0
You can find the Content-encoding item from header information is Gzip.
The solution is relatively simple, is to use curl instead of file_get_contents to get, and then in the Curl configuration parameters, add a code as follows:
Copy Code code as follows:
curl_setopt ($ch, curlopt_encoding, "gzip");
Today, with file_get_contents grasp the picture, the beginning did not find this problem, waste the eldest brother strength to find out.
Use the Zlib library, if the server has installed the Zlib library, with the following code can easily solve the garbled problem, the code is as follows:
Copy Code code as follows:
$data = file_get_contents ("compress.zlib://". $url);
I hope this article will help you with your PHP program design.