Home  >  Article  >  Backend Development  >  How to solve the problem of garbled characters using PHP's file_get_contents function

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

WBOY
WBOYOriginal
2016-07-06 13:54:091082browse

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

var_dump($str);
</code>

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>

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>

Find a way

<code>file_get_contents("compress.zlib://".$url);</code>
Statement:
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