更新记录时来自 PHP/MySQL 的平面 HTML 文件
我的 MySQL 数据库中只有几千条记录,可以通过 php/url 参数 (page.php?id=123) 动态调用。我还有一个粗糙的遗留函数,它从数据库中提取该页面的所有信息,并根据每条记录的 title 字段生成一个平面 .html 文件。每次更新记录时,都会运行此函数并重新生成页面。
它工作得很好,但是随着即将进行的重大重写以及上述函数中大量混合的 html/php 和可怕的条件逻辑,我希望能够更新 php 页面(在没有该函数的情况下也可以完美工作),并且然后,当后端更新记录时,会调用一个简单的命令来命中 page.php?id=updated_id 并在 /pages 目录中输出一个 HTML 页面,用于例子。
我正在考虑尝试 wget,但在尝试 PHP 中的系统内容之前,我想看看是否有人做过类似的事情?
目标是,我可以编辑 php 文件并将更新后函数更改为指向 page-v1.php,而不是更新这个每次设计更改时都会写入 html 的疯狂函数。 。
I have only a couple thousand records in a MySQL db that can be called dynamically via php/url params (page.php?id=123). I also have a gnarly legacy function that pulls all the info for that page from the db, and generates a flat .html file based on a title field for each record. Every time a record is updated, this function is run and the page is regenerated.
It works well enough, but with a major rewrite around the corner and tons of intermingling html/php and hideous conditional logic in said function, I'd like to be able to update the php page (which works perfectly without the function), and then as records are updated in the backend, a simple command is called that hits page.php?id=updated_id and spits out an HTML page in the /pages directory, for example.
I was thinking about trying wget but before I go trying system stuff in PHP I thought I'd see if anyone has done anything similar?
The goal is, instead of updating this crazy function that writes html every time there is a design change, I can just edit the php file and change the post-update function to point to page-v1.php.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你真的需要走生成 HTML 然后返回它的路线吗?这是创建 v.fast 站点的一个很好的解决方案,因为每个用户请求只需做很少的工作。
实现类似功能的更常见方法是向应用程序添加一定程度的缓存,或者在 Web 服务器前面放置反向代理(可能是 varnish、lighttpd、squid)。
编辑:如果只是想从另一个页面中请求 URL,则可以使用 file_get_contents() 或任何其他 PHP“文件”命令(如果启用了 url 包装器)来执行此操作。如果您想在 PHP 中使用更好、更可配置的 HTTP 客户端,请查看 cURL
Do you really need to go the route of generating the HTML and then returning it. It's a good solution for creating a v.fast site as there's little work done for each user request.
A more normal way of achieving something similar is to add some level of caching either to your application or by putting a reverse proxy (maybe varnish, lighttpd, squid) in front of the web server.
edit: If it's just a case of wanting to request a URL from within another page you can do this with
file_get_contents()
or any other PHP "file" commands if you have url wrappers enabled. If you want a nicer, more configurable HTTP client from within PHP take a look at cURL除非我遗漏了一些东西,否则你应该能够使用 include();这会将讨厌的文件放在您想要的任何地方。
如果这不起作用并且您仍然需要 wget 风格的功能,请查看 readfile() (它将采用 URL 作为参数)或curl 函数。
Unless I am missing something you should be able to use include(); That will drop the nasty file in wherever you want it.
If that does not work and you still want wget style features look at readfile() (which will take a URL as a parameter) or the curl functions.