Append HTML to page after redirect

拈花ヽ惹草 提交于 2019-12-11 07:32:29

问题


I'm using PHP to parse the contents of a web page: www.example.com/blahblah.

I'd like to append some HTML and a JS function to the bottom of this code that I collect. The JS function and HTML is on my server in a file called append.txt. The problem is that the wepage I'm parsing from contains several redirections, so the code I append does not end up at the final redirected web page. This is basically what I'm doing:

<?php
$html = file_get_contents('www.example.com/blahblah');
$append = file_get_contents('myserverdirectory/append.txt');

echo $html . $append; //Append code to the parsed code and display it 
?>

I've tested this on pages that don't get redirected and it is perfect. How can I append the code to the FINAL redirected page? Obviously the current code just appends it to the first page and after all the redirections my appended code is gone on subsequent pages. I can't just parse/scrape the final redirected page by the way...I wish it was that simple haha. I've tried using cURL for the redirects but it seems to do exactly the same thing...any ideas? It would be great if I could append my code to the webpage code after the redirects finish or something...I guess the major issue is that the echo and redirection in the echo'd code is causing it to exit the PHP.

Thanks for the help!

EDIT: As I said, I've tried using cURL to follow the redirects. This is useless and so is just using file_get_contents on the final web page. For some reason it needs all those redirects. I used this for the cURL implementation:

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

$html = file_get_contents_curl('www.example.com/blahblah'); // This url is the first page before it does all the re-directing

来源:https://stackoverflow.com/questions/20455827/append-html-to-page-after-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!