我编写了一个简单的内容用户,它使用了file_get_contents,但不幸的是,对于我的IP,该网站现在提供了一个转发到图像的302错误.对于所有其他用户,可以查看普通站点.
如何重写get_contents以便它只下载网站内容而不实际遵循重定向?
$html = file_get_contents("http://www.site.net/");
Run Code Online (Sandbox Code Playgroud)
lon*_*day 21
您需要创建一个上下文:
$context = stream_context_create(
array (
'http' => array (
'follow_location' => false // don't follow redirects
)
)
);
$html = file_get_contents('http://www.site.net/', false, $context);
Run Code Online (Sandbox Code Playgroud)
参见手册:
话虽如此,页面上很可能没有内容.提供302标头并服务于HTTP主体并非不可能,但它绝对是非正统的.