How to solve the problem of garbled characters using PHP's file_get_contents function

WBOY
Release: 2016-07-06 13:54:09
Original
1078 people have browsed it

<code>$url = "http://i.ifeng.com/";
$str = file_get_contents($url);

var_dump($str);
</code>
Copy after login
Copy after login

The result is garbled, how to fix it? Please help me, thank you very much!

Waiting online

Reply content:

<code>$url = "http://i.ifeng.com/";
$str = file_get_contents($url);

var_dump($str);
</code>
Copy after login
Copy after login

The result is garbled, how to fix it? Please help me, thank you very much!

Waiting online

There are two main reasons for garbled characters, one is the GZIP compression problem, and the other is the encoding problem. For PHP, it is not very suitable to use file_get_contents to get the content of a web page. I recommend using curl. Its stability is unparalleled, and it can be solved whether it is http or https.

Just set the encoding. You try it

<code>header("Content-Type:text/html;charset=utf-8");

$url = "http://i.ifeng.com/";
$str = file_get_contents($url);

var_dump($str);</code>
Copy after login

Find a way

<code>file_get_contents("compress.zlib://".$url);</code>
Copy after login
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!