我需要使用PHP来保存一个在iFrame中生成内容的网页,就像这样......
我有一个带有iFrame的.PHP文件(里面会打开一个产生动态内容的URL)。
我希望Php文件将生成的内容(或整个源)保存到服务器。
我尝试了@file_get_contents但是如何指定同一个.php文件的URL,因为它在iFrame中。?
另外,如何使用PHP将整个HTTP标头输出到文件中?
我知道它有点不清楚,但请耐心等待我!
我尝试了这段代码,但它不起作用。
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>
由于
答案 0 :(得分:3)
首先,永远不要使用file_get_contents()
URL,因为它在大多数(配置良好的)服务器上被禁用。您可以使用精彩的图书馆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);
?>
将完整页面存储到提供的文件中。
答案 1 :(得分:1)
您必须将javascript中的iframe网址发送回服务器
var url = document.getElementById("iframe_id").contentWindow.location.href;
然后您可以使用jquery将其发送回服务器
$.get('mywebpage.php?url='+url);
最后在file_get_contents($ _ GET ['url'])服务器端使用此URL。