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

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献