Php 为什么文件内容返回了乱码数据?
我正在尝试使用一些简单的php从下面的页面获取HTML 网址: 我的代码是:Php 为什么文件内容返回了乱码数据?,php,httpresponse,file-get-contents,inflate,Php,Httpresponse,File Get Contents,Inflate,我正在尝试使用一些简单的php从下面的页面获取HTML 网址: 我的代码是: $html = file_get_contents('https://kat.cr/usearch/architecture%20category%3Abooks/'); echo $html; 其中file\u get\u contents起作用,但返回加扰数据: 我尝试过使用cUrl以及各种函数,如:htmlentities(),mb\u convert\u encoding,utf8\u encode等等,但
$html = file_get_contents('https://kat.cr/usearch/architecture%20category%3Abooks/');
echo $html;
其中file\u get\u contents
起作用,但返回加扰数据:
我尝试过使用cUrl
以及各种函数,如:htmlentities(),
mb\u convert\u encoding
,utf8\u encode
等等,但只是得到了不同的置乱文本变体
该页面的来源说它是charset=utf-8
,但我不确定问题出在哪里
在基本urlkat.cr
上调用file\u get\u contents()
我在这里遗漏了什么?它是GZ压缩的,当通过浏览器获取时,浏览器会将其解压缩,因此您需要解压缩。要同时输出,您可以使用:
它是GZ压缩的,当通过浏览器获取时,浏览器会对其进行解压缩,因此需要解压缩。要同时输出,您可以使用:
您的站点响应正在被压缩,因此您必须解压缩以将其转换为原始形式
最快的方法是按如下方式使用:
$html = gzinflate(substr(file_get_contents("https://kat.cr/usearch/architecture%20category%3Abooks/"), 10, -8));
或对于更高级的解决方案,请考虑以下函数(在这里找到):
您的站点响应正在被压缩,因此您必须解压缩以将其转换为原始形式
最快的方法是按如下方式使用:
$html = gzinflate(substr(file_get_contents("https://kat.cr/usearch/architecture%20category%3Abooks/"), 10, -8));
或对于更高级的解决方案,请考虑以下函数(在这里找到):
看看这个。看:刮削洪流网站,有点低。看看这个。看:刮削洪流网站,有点低谢谢!简单有效,谢谢!简单有效。
function get_url($url)
{
//user agent is very necessary, otherwise some websites like google.com wont give zipped content
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-Language: en-US,en;q=0.8rn" .
"Accept-Encoding: gzip,deflate,sdchrn" .
"Accept-Charset:UTF-8,*;q=0.5rn" .
"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn"
)
);
$context = stream_context_create($opts);
$content = file_get_contents($url ,false,$context);
//If http response header mentions that content is gzipped, then uncompress it
foreach($http_response_header as $c => $h)
{
if(stristr($h, 'content-encoding') and stristr($h, 'gzip'))
{
//Now lets uncompress the compressed data
$content = gzinflate( substr($content,10,-8) );
}
}
return $content;
}
echo get_url('http://www.google.com/');