2016-10-10 94 views
0

我想解析一个网站的主页,但它只能通过从另一个页面重定向访问,所以我只能拥有重定向页面的html。获取“重定向到”页面

如何获取“重定向到”页面的html页面?

以下是一个例子: 我可以得到一个页面a.html,当我用浏览器打开它会将我重定向到b.html,我想解析b.html,但是当我打开b.html直接需要重定向时可以从a.html发送到b.html的POST参数。

编辑:只是为了说明,“重定向到”页面有一个相对路径,所以我执行以下操作:

$pos=strpos($result,"window.location = \""); 
$res= substr_replace ($result,"https://thecompletepath/",$pos,0); 
echo $res; 

和重定向是通过javascript代码,如下:

<script type="text/javascript" charset="utf-8"> 
    escapeIfModal(); 
    LoadingScreen.start(); 
    window.location = "/home"; 
</script> 
+0

如何发布这些参数b类似的一个是干什么的? ;) –

+0

检查这一个:http://stackoverflow.com/questions/3519939/make-curl-follow-redirects –

+0

@jakubwrona我怎么知道发送的所有POST参数? –

回答

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); 
+0

重定向是通过JavaScript代码(我已更新问题),虽然该方法没有奏效。 –

+0

然后我会建议你确切地检查哪些参数被发送到“重定向到”页面并完全使用curl或file_get_contents模仿请求 –

+0

因此,如果没有正确应用JS代码,重定向是不可能的?我如何检查这些参数? –