Heim  >  Artikel  >  Backend-Entwicklung  >  用 PHP 的 file_get_contents 函数出现乱码怎么破

用 PHP 的 file_get_contents 函数出现乱码怎么破

WBOY
WBOYOriginal
2016-07-06 13:54:091077Durchsuche

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

var_dump($str);
</code>

结果乱码了,怎么破?求大神帮助,万分感激!

在线等

回复内容:

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

var_dump($str);
</code>

结果乱码了,怎么破?求大神帮助,万分感激!

在线等

乱码的原因主要就是两个,一个是GZIP压缩的问题,一个是编码的问题。对PHP来说,使用file_get_contents来取一个网页内容并不是非常合适,我推荐使用curl来做。它的稳定性是无与伦比的,不管是http还是https都可以解决。

设置一下编码就行了。你试试看

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

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

var_dump($str);</code>

找到方法了

<code>file_get_contents("compress.zlib://".$url);</code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn