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

标签 php curl file-get-contents

我对这个网站和 php->file_get_contents 或 php->curl 或 bash->wget 有一个奇怪的问题。

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

使用普通浏览器(chrome、konqueror 等,即使在隐身模式下,因此这不依赖于“登录”问题),页面会正确下载。链接为:

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 的结果相同。

有什么想法吗? 问候。

最佳答案

无论出于何种原因,当请求中不存在 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');

关于php - file_get_contents()、curl 和 wget 不适用于此站点(它们返回 "HNGJpP5b-452"字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62742265/

相关文章:

php - 是否可以在App :before filter in Laravel?中返回

javascript - 单击特定按钮后如何呈现特定 View 。 PHP HTML JS

python-3.x - 如何使用 python 发布curl命令

phpcurl ssl验证

php - 从 file_get_contents 请求获取 IP

php - 使用 Facebook SDK 从 iOS 登录 Laravel webapp

php - 如何在 Symfony2 中禁用一个转换器验证错误 "dynamically"

curl - 如果文件大小超过内容长度,libcurl 是否会中止下载?

PHP file_get_contents 将 & 转换为 &amp

php - file_get_contents() 是否有超时设置?