Php file_get_contents()、curl和wget don';t与该网站合作(他们返回“HNGJpP5b-452”字符串)
我对这个网站和php->file\u get\u contents或php->curl或bash->wget有一个奇怪的问题 如果我尝试下载这个页面,我会得到一个只包含字符串HNGJpP5b-452的小文件 对于普通浏览器(chrome、konqueror和其他浏览器,即使在匿名模式下,这也不取决于“登录”问题),页面被正确下载。链接是:Php file_get_contents()、curl和wget don';t与该网站合作(他们返回“HNGJpP5b-452”字符串),php,curl,file-get-contents,Php,Curl,File Get Contents,我对这个网站和php->file\u get\u contents或php->curl或bash->wget有一个奇怪的问题 如果我尝试下载这个页面,我会得到一个只包含字符串HNGJpP5b-452的小文件 对于普通浏览器(chrome、konqueror和其他浏览器,即使在匿名模式下,这也不取决于“登录”问题),页面被正确下载。链接是: link = https://rutracker.net/forum/viewforum.php?f=1992 我使用了以下php代码: <? $l
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";
}
?>
这似乎不是因为“用户代理”,对于curl,我尝试将相对选项CURLOPT_USERAGENT设置为与chrome相同,没有任何更改
bash中wget的结果相同
有什么想法吗?
关于。无论出于何种原因,当请求中不存在
接受编码
标题时,本网站将返回该字符串
您可以使用流上下文将Accept Encoding
头添加到file\u get\u contents()
$context = stream_context_create([
"http" => [
"header" => "Accept-Encoding: gzip,deflate,br\r\n"
]
]);
$content = file_get_contents($lnk, false, $context);
或者使用
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,br');
是的,很有效,谢谢。现在“example.com”给我一个zlib编码内容和文件获取内容方法。所以我添加了$h=file\u get\u contents($lnk,false,$context)$html=zlib_解码($h);如果($html==FALSE)$html=$h;
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,br');