The example in this article describes how to use file_get_contents in PHP to crawl Chinese garbled web pages. Share it with everyone for your reference. The specific method is as follows:
The file_get_contents function is originally a very excellent local and remote file operation function that comes with PHP. It allows us to download remote data directly without any effort, but I encountered some problems when using it to read web pages. The page is garbled. Here we will summarize the specific solutions for you.
According to friends on the Internet, the reason may be that the server has turned on GZIP compression. The following is to use firebug to check the header information of my website. Gzip is turned on, and the original header information of the request header information is as follows:
The solution is relatively simple, which is to use curl instead of file_get_contents to obtain, and then add one to the curl configuration parameters. The code is as follows:
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.
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:
I hope this article will be helpful to everyone’s PHP programming design.