简体   繁体   中英

Use PHP to Save Webpage Content on the same URL

I need to use PHP to save a webpage which has content generated in an iFrame, Its like this..

I have a .PHP file with an iFrame (inside which it opens a URL which produces dynamic content) inside it.

I want the Php file to save the generated content (or the whole source) to the server.

I tried the @file_get_contents but how do I specify the URL of the same .php file since it is in iFrame..?

Also how can I output the entire HTTP header into a file with PHP?

I know its a bit unclear but bear with me please!

I tried this code but it doesn't work.

CODE

<html>
<body>
<?php

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
$contents = @file_get_contents($pageURL);
$fp = fopen("file.txt", "a");
fputs($fp, "
$contents
");
fclose($fp);
?>
<iframe src="LINK TO WEBPAGE HERE" />
</body>
</html>

Thanks

First of all never use file_get_contents() wih URLS because it is disabled on most (well configured) servers. You can use a fantastic library cURL

http://php.net/manual/en/book.curl.php

<?php

$ch = curl_init("http://HREF of iframe here");
$fp = fopen("some filename name here", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

which will store complete page into provided file.

You have to send the url of the iframe in javascript back to the server

var url = document.getElementById("iframe_id").contentWindow.location.href;

Then you can use jquery for example to send it back to the server

$.get('mywebpage.php?url='+url);

And finally use this url in a file_get_contents($_GET['url']) server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM