-1

我需要使用 PHP 来保存在 iFrame 中生成内容的网页,就像这样..

我有一个 .PHP 文件,里面有一个 iFrame(它在其中打开一个生成动态内容的 URL)。

我希望 PHP 文件将生成的内容(或整个源)保存到服务器。

我尝试了@file_get_contents,但是我如何指定同一个 .php 文件的 URL,因为它在 iFrame 中..?

另外,如何使用 PHP 将整个 HTTP 标头输出到文件中?

我知道它有点不清楚,但请多多包涵!

我试过这段代码,但它不起作用。

代码

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

谢谢

4

2 回答 2

3

首先永远不要使用file_get_contents()wih URLS,因为它在大多数(配置良好的)服务器上被禁用。你可以使用一个很棒的图书馆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);
?>

它将完整的页面存储到提供的文件中。

于 2013-03-03T18:34:01.240 回答
1

您必须将 javascript 中 iframe 的 url 发送回服务器

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

然后您可以使用 jquery 例如将其发送回服务器

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

最后在 file_get_contents($_GET['url']) 服务器端使用这个 url。

于 2013-03-03T17:58:01.137 回答