file_get_contents()、curl 和 wget 不适用于此站点(它们 return "HNGJpP5b-452" 字符串)

file_get_contents(), curl and wget don't works with this site (they return "HNGJpP5b-452" string)

我对这个站点和 php->file_get_contents 或 php->curl 或 bash->wget.[=16= 有一个奇怪的问题]

如果我尝试下载此页面,我会得到一个仅包含字符串 HNGJpP5b-452 的小文件。

使用普通浏览器(chrome、konqueror 和其他浏览器,即使是在隐身模式下,所以这不取决于“登录”问题),页面可以正确下载。 link 是:

link = https://rutracker.net/forum/viewforum.php?f=1992

我用过这个php代码:

<?

$lnks = array("https://rutracker.net/forum/viewforum.php?f=1992", "https://example.com");

foreach($lnks as $lnk) {
    echo "Working with url: ".$lnk."<br>\n";
    echo "========================================================================<br>\n";
    // file_get_contents part
    $html=file_get_contents($lnk);
    echo "file_get_contents get this: ".$html."<br>\n<br>\n";

    // curl part
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $lnk);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $html = curl_exec($ch);
    echo "curl get this: ".$html."<br>\n<br>\n";
}

?>

结果是:

Working with url: https://rutracker.net/forum/viewforum.php?f=1992
========================================================================
file_get_contents get this: HNGJpP5b-452

curl get this: HNGJpP5b-452

Working with url: https://example.com
========================================================================
file_get_contents get this:
Example Domain
This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.

More information...



curl get this:
Example Domain
This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.

More information...

这似乎不是由于“用户代理”造成的,对于curl,我尝试将相关选项CURLOPT_USERAGENT设置为与chrome相同,没有任何更改。

bash 中 wget 的相同结果。

有什么想法吗? 问候。

无论出于何种原因,此网站 returns 请求中不存在 Accept-Encoding header 时的字符串。

您可以使用流上下文将 Accept-Encoding header 添加到 file_get_contents()

$context = stream_context_create([
    "http" => [
        "header" => "Accept-Encoding: gzip,deflate,br\r\n"
    ]
]);

$content = file_get_contents($lnk, false, $context);

或使用

的 curl 请求
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,br');