简体   繁体   English

file_get_contents返回特定URL的不可读文本

[英]file_get_contents returns unreadable text for a specific url

When I try to read the rss feeds of the kat.cr using php file_get_contents function, I get some unreadable text but when I open it up with my browser the feed is fine. 当我尝试使用php file_get_contents函数读取kat.cr的rss提要时,我得到了一些不可读的文本,但是当我用浏览器打开它时,提要很好。
I have tried many other hosts but no chance in getting the correct data. 我尝试了许多其他主机,但是没有机会获得正确的数据。
I even have tried setting the user-agent to diffrent browsers but still no change. 我什至尝试将用户代理设置为不同的浏览器,但仍然没有变化。
this is a simple code that I've tried: 这是我尝试过的简单代码:

$options  = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'));
$url = 'https://kat.cr/movies/?rss=1';
$data = file_get_contents($url, FILE_TEXT, stream_context_create($options));
echo $data;

I'm curious how their doing it and what I can do to overcome the problem. 我很好奇他们的工作方式以及如何解决这个问题。

A part of unreadable text: 不可读文字的一部分:

‹ي]يrم6–‎?Oپي©™ت,à7{»‌âgw&يؤe;éN¹\\S´HK\\S¤–¤l+ے÷ِùِIِ”(إژzA5‌ةض؛غ%K4ـ{qtqy½ùوa^ »¬nٍھ|ûٹSِ eه¤Jَrِْصڈ1q^}sü§7uسlدزؤYً¾²yفVu‌•يغWGG·Iس&m>،“j~$ےzؤ(?zï‍ج'²جٹم?!ّ÷¦حغ";‏گ´Yس¢ï³{tر5ز ³َsgYٹْ.ں@ ‹ي]يr م6–?Oپي©™ت,à7{»‌âgw&يؤe;éN¹\\ S´HK \\ S¤–¤l +ے÷ےùِ I ِ”(إژzA5‌ةض؛غ%K4 ـ {qtqy½ùوa ^»¬nٍھ |ûٹS ِ eه¤J َ r ِْصڈ 1 q ^}sü§7uسlدزؤY ً¾² yفVu‌•يغWGG·Iس&m>,“ j〜$ےzؤ(?zï‍ج'²جٹم?!ّ÷¦حغ“; گ´Yس¢ï³{tر5ز³َ sgY ٹْ.ں @

Actually everytime I open up the link there is some different unreadable text. 实际上,每次我打开链接时,都会有一些不同的不可读文本。

As I mentioned in the comment - the contents returned are gzip encoded so you need to un-gzip the data. 正如我在评论中提到的那样-返回的内容是gzip编码的,因此您需要解压缩数据。 Depending upon your version of php you may or may not have gzdecode installed, I don't but the function here does the trick. 根据您的php版本,您可能会或可能不会安装gzdecode ,但我没有,但是这里的功能可以解决问题。

if( !function_exists('gzdecode') ){
    function gzdecode( $data ){ 
        $g=tempnam('/tmp','ff'); 
        @file_put_contents( $g, $data );
        ob_start();
        readgzfile($g);
        $d=ob_get_clean();
        unlink($g);
        return $d;
    }   
}
$data=gzdecode( file_get_contents( $url ) );
echo $data;     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM