可以使用file_get_contents函数的第三个参数来获取重定向后的页面内容。
示例代码如下:
$url = 'https://example.com'; // 重定向前的URL
$context = stream_context_create([
'http' => [
'follow_location' => true, // 启用重定向
'max_redirects' => 10 // 最大重定向次数
]
]);
$content = file_get_contents($url, false, $context);
echo $content;
在上述示例中,我们创建了一个$context上下文,并通过stream_context_create函数将其传递给file_get_contents函数。$context的http选项中设置了follow_location为true,表示启用重定向,以及max_redirects表示最大重定向次数。
然后,我们使用file_get_contents函数来获取重定向后的页面内容,并将其存储在$content变量中。最后,我们使用echo语句来输出获取到的内容。
请注意,使用file_get_contents函数获取远程内容需要确保相关配置已经正确设置,并且服务器允许该操作。如果无法获取重定向后的页面内容,可以考虑使用其他方法,如cURL库。