如何使用 php 从 php 脚本的输出生成静态 html 文件
我想从 php 文件生成静态 html 页面并从其他 php 脚本保存它。该脚本运行一堆 echo 函数,当在浏览器中查看时,这是一个漂亮的 html 页面。但是当我运行 file_get_contents 时,它会将该文件作为文件系统上的文件打开,而不是作为 url 中的文件打开。
我需要以 localhost/site/categories.php 方式调用 file_get_contents 吗?我怎样才能得到这条路径?这是错误的代码:
<?php
$file = file_get_contents("categories.php");
file_put_contents("categories.html", $file);
?>
I would like to generate a static html page from a php file and save it from an other php script. That script runs a bunch of echo functions, which when viewed in a browser is a nice html page. But when I run file_get_contents it opens that file as a file on the filesystem, not as a file in an url.
Do I need to call file_get_contents in a localhost/site/categories.php way? How can I get this path? This is the wrong code:
<?php
$file = file_get_contents("categories.php");
file_put_contents("categories.html", $file);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要获得最终的输出,您需要使用 PHP url 包装器功能并通过网络服务器请求它。然后就很简单了:
To get the finished output, you need to use the PHP url wrappers functionality and request it over the webserver. Then it's as easy as:
是的 - 以本地主机的方式运行 file_get_contents - 如果您的服务器配置正确,它不会长途跋涉到互联网,并且会以有效的方式获取结果,即使托管在您自己的域名上也是如此。
Yes - run file_get_contents in a localhost way - if your server is configured correctly, it will not trek off to the internet and will get your results in an efficient manner, even when hosted on your own domain name.
我相信你可以简单地:
但是, fopen 包装器必须启用 才能使
file_get_contents()
读取 URL。I believe you can simply:
However, the fopen wrappers must be enabled for
file_get_contents()
to read URLs.出于安全原因,我不会使用 php 来完成。更好的方法是使用 ssh 将文件复制到所需的远程服务器:
I wouldn't use php to accomplish for security reasons. A better approach would be to use ssh to copy the file to the desired remote server:
我可能没有注意到这里的要点,但是 include() 另一个 PHP 文件并简单地使用它来直接按需创建输出,而不是在该过程的该阶段完全涉及网络服务器,这也可能是一个想法。
I may be missing the point here, but it might also be an idea to include() the other PHP file and simply use it to create the output directly on demand instead of involving the webserver at all for that stage in the process.
对我来说,它被 webhook 用于创建另一个由 API 驱动的文件。它用于减少 API 请求的数量。
For me its being used by a webhook to create another file thats API driven. Its used to reduce the number of API requests.