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
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>));
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:
<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>
Use gzip encoding format
<span>file_get_contents</span>("compress.zlib://".<span>$url</span>);
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>
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.