dongtou8736 2016-10-10 12:06
浏览 194
已采纳

获取“重定向到”页面

I am trying to parse a home page of a site, but it is accessible through redirecting from another page only, so I can only have the html of the redirecting page.

How can I get the html page of the "redirected to" page ?

the following is an example: I can get a page a.html, which when I open with browser it will redirect me to b.html, I want to parse b.html, but when I open b.html directly it will require POST parameters that can be sent from a.html to b.html when redirecting.

Edit: just for note, the "redirected to" page is has a relative path, so I do the following:

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

and the redirecting is through a javascript code, as following:

<script type="text/javascript" charset="utf-8">
    escapeIfModal();
    LoadingScreen.start();
    window.location = "/home";
</script>
  • 写回答

1条回答 默认 最新

  • dongyi6195 2016-10-10 12:14
    关注

    You can use cURL to follow redirects as the browser would.

    $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"
    

    using 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改