php 将当前页面另存为 HTML 到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3775281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save current page as HTML to server
提问by HoLyVieR
What approach could someone suggest to save the current page as an HTML file to the server? In this case, also note that security is notan issue.
有人建议用什么方法将当前页面作为 HTML 文件保存到服务器?在这种情况下,还要注意安全性不是问题。
I have spent endless hours searching around for this, and have not found a single thing.
我花了无数个小时寻找这个,但没有找到任何东西。
Your help is much appreciated, thank you!
非常感谢您的帮助,谢谢!
Edit
编辑
Thank you all for your help, it was very much appreciated.
谢谢大家的帮助,非常感谢。
回答by HoLyVieR
If you meant saving the output of a page in a file, you can use buffering to do that. The function you need to use are ob_startand ob_get_contents.
如果您打算将页面的输出保存在文件中,您可以使用缓冲来做到这一点。您需要使用的函数是ob_start和ob_get_contents。
<?php
// Start the buffering //
ob_start();
?>
Your page content bla bla bla bla ...
<?php
echo '1';
// Get the content that is in the buffer and put it in your file //
file_put_contents('yourpage.html', ob_get_contents());
?>
This will save the content of the page in the file yourpage.html
.
这会将页面内容保存在文件中yourpage.html
。
回答by Chetan Sharma
I think we can use Output Control Functionsof PHP, you can use save the content to the variable first and then save them to the new file, next time, you can test it the html file exists, then render that else re-generate the page.
我想我们可以使用PHP的输出控制函数,可以先将内容保存到变量中,然后再保存到新文件中,下次可以测试html文件是否存在,然后渲染,否则重新生成页。
<?php
$cacheFile = 'cache.html';
if ( (file_exists($cacheFile)) && ((fileatime($cacheFile) + 600) > time()) )
{
$content = file_get_contents($cacheFile);
echo $content;
} else
{
ob_start();
// write content
echo '<h1>Hello world to cache</h1>';
$content = ob_get_contents();
ob_end_clean();
file_put_contents($cacheFile,$content);
echo $content;
}
?>
Example taken from : http://www.php.net/manual/en/function.ob-start.php#88212
示例取自:http: //www.php.net/manual/en/function.ob-start.php#88212
回答by BalusC
Use JavaScript to send document.getElementsByTagName('html')[0].innerHTML
as hidden input value or by ajax to the server side. This is more useful than output buffering if the content is afterwards traversed/modified by JavaScript, which the server side might not have any notion about.
使用 JavaScriptdocument.getElementsByTagName('html')[0].innerHTML
作为隐藏输入值或通过 ajax 发送到服务器端。如果内容随后被 JavaScript 遍历/修改,这比输出缓冲更有用,服务器端可能对此没有任何概念。
回答by Sarfraz
In case you are looking to save complete html page along with css, images and scripts in a single html file, you can use this class I have written:
如果您希望在单个 html 文件中保存完整的 html 页面以及 css、图像和脚本,您可以使用我编写的这个类:
This class can save HTML pages complete with images, CSS and JavaScript.
It takes the URL of a given page and retrieves it to store in a given file.
The class can parse the HTML and determine which images, CSS and JavaScript files it needs, so those files are also downloaded and saved inside the HTML page saved to a local file.
Optionally it can skip the JavaScript code, keep only the page content, and compress the resulting page removing the whitespace.
这个类可以保存带有图像、CSS 和 JavaScript 的 HTML 页面。
它获取给定页面的 URL 并检索它以存储在给定文件中。
该类可以解析 HTML 并确定它需要哪些图像、CSS 和 JavaScript 文件,因此这些文件也会被下载并保存在保存到本地文件的 HTML 页面中。
它可以选择跳过 JavaScript 代码,仅保留页面内容,并压缩结果页面以去除空格。
http://www.phpclasses.org/package/8305-PHP-Save-HTML-pages-complete-with-images-CSS-and-JS.html
http://www.phpclasses.org/package/8305-PHP-Save-HTML-pages-complete-with-images-CSS-and-JS.html
回答by shaan gola
//function to use curl to get the content of the page.
//parameter used url and $data for the posting credentials to retrieve information.
function httpPost($url, $data){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
//
$filename="abc.html"; // whatever name you want.
$myfile = fopen($filename, "w") or die("Unable to open file!");
$txt = httpPost(<url>, ""); //<url> replace by url you want.
fwrite($myfile, $txt);
fclose($myfile);
回答by Satya Prakash
I feel you need curl, so that you can save any pages' output. Use curl with returntransfer true. and do whatever you want with the output.
我觉得你需要 curl,这样你就可以保存任何页面的输出。将 curl 与 returntransfer true 一起使用。并对输出做任何你想做的事情。