PHP Use file_get_contents Crawl Web page Chinese garbled problem solving method,
In this paper, we explain how to solve the problem of using file_get_contents to crawl Web pages in PHP. Share to everyone for your reference. Here's how:
file_get_contents function is originally a very good PHP with local and remote file operation function, it can let us do not spend the power to download remote data directly, but I use it to read the page will encounter some of the 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 a firebug view of my website header information, gzip Open, request header information Raw header information, the code is as follows:
Copy the 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
The content-encoding item that can be found from the header information is Gzip.
The solution is relatively simple, is to use curl instead of file_get_contents to get, and then add one in the Curl configuration parameters, the code is as follows:
Copy the 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 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 the code as follows: $data = file_get_contents ("compress.zlib://". $url);
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/929093.html www.bkjia.com true http://www.bkjia.com/PHPjc/929093.html techarticle PHP use file_get_contents crawl Web page Chinese garbled problem solving method, this article describes the PHP use file_get_contents Crawl Web page Chinese garbled problem solving method. Share to the big ...