Home > Backend Development > PHP Tutorial > The garbled problem when reading remote files with php file_get_contents is caused by gzip compression.

The garbled problem when reading remote files with php file_get_contents is caused by gzip compression.

WBOY
Release: 2016-07-29 09:06:53
Original
1168 people have browsed it

  Yesterday I found that the weather api of China Weather Network that I made before was saved locally and I found that some cities were garbled.

I still can’t find the reason. Because it looks completely normal in the browser. like. Read Yinchuan City's weather for the day http://m.weather.com.cn/data/101170101.html

php file_get_contents读取远程文件的乱码问题gzip压缩引起的

and view its json data in the browser. It is completely normal. Encoding is also normal. But using file_get_contents to read the output in the browser is garbled.

<span>$url</span> = 'http://m.weather.com.cn/data/101170101.html'<span>;
</span><span>echo</span> '<pre class="brush:php;toolbar:false">'<span>;
</span><span>print_r</span>(<span>file_get_contents</span>(<span>$url</span>));
Copy after login

php file_get_contents读取远程文件的乱码问题gzip压缩引起的

I was busy online for a while and found out the reason: China Weather Network turned on gzip compression. Found a solution from http://www.php10086.com/2012/03/516.html

PHP's file_get_contents gets the remote page content. If it is gzip encoded, the returned string is encoded garbled code. How? There are two ways to solve the gzip problem:

curl solution:

php file_get_contents读取远程文件的乱码问题gzip压缩引起的

<span>function</span> curl_get(<span>$url</span>, <span>$gzip</span>=<span>false</span><span>){
        </span><span>$curl</span> = curl_init(<span>$url</span><span>);
        curl_setopt(</span><span>$curl</span>, CURLOPT_RETURNTRANSFER, 1<span>);
        curl_setopt(</span><span>$curl</span>, CURLOPT_CONNECTTIMEOUT, 10<span>);
        </span><span>if</span>(<span>$gzip</span>) curl_setopt(<span>$curl</span>, CURLOPT_ENCODING, "gzip"); <span>//</span><span> 关键在这里</span><span>$content</span> = curl_exec(<span>$curl</span><span>);
        curl_close(</span><span>$curl</span><span>);
        </span><span>return</span><span>$content</span><span>;
}</span>
Copy after login

php file_get_contents读取远程文件的乱码问题gzip压缩引起的

Use gzip encoding format

file_get_contents solution :

<span>file_get_contents</span>("compress.zlib://".<span>$url</span>);
Copy after login

No matter the page The above code works fine with or without gzip compression!

Supported by PHP 4.3.0 and later, and can also be used for functions like fopen~!

Solution:

<span>$url</span> = 'http://m.weather.com.cn/data/101170101.html'<span>;

</span><span>echo</span> '<pre class="brush:php;toolbar:false">'<span>; 

</span><span>print_r</span>(<span>file_get_contents</span>("compress.zlib://".<span>$url</span>));<span>//</span><span>打开gzip压缩过的页面。 路径前不加compress.zlib:// 打开会有乱码。  </span>
Copy after login

The above introduces the garbled problem of php file_get_contents reading remote files caused by gzip compression, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template