我正在尝试解析网站的主页,但只能通过从另一个页面重定向来访问它,所以我只能拥有重定向页面的html。
如何获取&#34的html页面;重定向到"页面?
以下是一个例子: 我可以获得一个页面a.html,当我用浏览器打开它时会将我重定向到b.html,我想解析b.html,但是当我直接打开b.html时,它将需要可以发送的POST参数重定向时a.html到b.html。
编辑:仅供注意,"重定向到"页面有一个相对路径,所以我做了以下几点:
IQKeyboardManager
,重定向是通过javascript代码,如下所示:
$pos=strpos($result,"window.location = \"");
$res= substr_replace ($result,"https://thecompletepath/",$pos,0);
echo $res;
答案 0 :(得分:1)
您可以使用cURL跟踪浏览器的重定向。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "a.html");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch); //response $a would contain the last redirected location: "b.html"
使用file_get_contents:
$context = stream_context_create(
array(
'http' => array(
'follow_location' => true
)
)
);
$html = file_get_contents('http://www.example.com/a.html', false, $context);