简体   繁体   English

如何在 php 中创建站点并让它生成 static 版本?

[英]How can I create a site in php and have it generate a static version?

For a particular project I have, no server side code is allowed.对于我拥有的特定项目,不允许使用服务器端代码。 How can I create the web site in php (with includes, conditionals, etc) and then have that converted into a static html site that I can give to the client? How can I create the web site in php (with includes, conditionals, etc) and then have that converted into a static html site that I can give to the client?

Update: Thanks to everyone who suggested wget.更新:感谢所有建议 wget 的人。 That's what I used.这就是我用的。 I should have specified that I was on a PC, so I grabbed the windows version from here: http://gnuwin32.sourceforge.net/packages/wget.htm .我应该指定我在 PC 上,所以我从这里获取了 windows 版本: http://gnuwin32.sourceforge.net/packages/wget.htm

If you have a Linux system available to you use wget :如果您有可用的 Linux 系统,请使用wget

wget -k -K  -E -r -l 10 -p -N -F -nH http://website.com/

Options选项

  • -k: convert links to relative -k:将链接转换为相对链接
  • -K: keep an original versions of files without the conversions made by wget -K:保留文件的原始版本,没有 wget 进行的转换
  • -E: rename html files to.html (if they don't already have an htm(l) extension) -E:将 html 文件重命名为 .html(如果它们还没有 htm(l) 扩展名)
  • -r: recursive… of course we want to make a recursive copy -r: recursive……当然我们想要做一个递归副本
  • -l 10: the maximum level of recursion. -l 10:最大递归级别。 if you have a really big website you may need to put a higher number, but 10 levels should be enough.如果你有一个非常大的网站,你可能需要放一个更高的数字,但 10 级应该足够了。
  • -p: download all necessary files for each page (css, js, images) -p:为每个页面下载所有必要的文件(css、js、图像)
  • -N: Turn on time-stamping. -N:开启时间戳。
  • -F: When input is read from a file, force it to be treated as an HTML file. -F:从文件中读取输入时,强制将其视为 HTML 文件。
  • -nH: By default, wget put files in a directory named after the site's hostname. -nH:默认情况下,wget 将文件放在以站点主机名命名的目录中。 This will disabled creating of those hostname directories and put everything in the current directory.这将禁止创建这些主机名目录并将所有内容放在当前目录中。

Source: Jean-Pascal Houde's weblog资料来源: Jean-Pascal Houde 的博客

build your site, then use a mirroring tool like wget or lwp-mirror to grab a static copy构建您的站点,然后使用wgetlwp-mirror等镜像工具获取 static 副本

I have done this in the past by adding:我过去通过添加:

ob_start();

In the top of the pages and then in the footer:在页面顶部,然后在页脚中:

$page_html = ob_get_contents();
ob_end_clean();
file_put_contents($path_where_to_save_files . $_SERVER['PHP_SELF'], $page_html);

You might want to convert.php extensions to.html before baking the HTML into the files.在将 HTML 烘焙到文件之前,您可能需要将 .php 扩展名转换为 .html 。 If you need to generate multiple pages with variables one quite easy option is to append the filename with md5sum of all GET variables, you just need to change them in the HTML too.如果您需要使用变量生成多个页面,一个非常简单的选项是 append 文件名和所有 GET 变量的 md5sum,您只需在 HTML 中更改它们。 So you can convert:所以你可以转换:

somepage.php?var1=hello&var2=hullo

to

somepage_e7537aacdbba8ad3ff309b3de1da69e1.html

ugly but works.丑陋但有效。

Sometimes you can use PHP to generate javascript to emulate some features, but that cannot be automated very easily.有时您可以使用 PHP 生成 javascript 来模拟某些功能,但这不能很容易地自动化。

Create the site as normal, then use spidering software to generate a HTML copy.正常创建站点,然后使用爬虫软件生成 HTML 副本。

HTTrack is software I have used before. HTTrack是我以前用过的软件。

One way to do this is to create the site in PHP as normal, and have a script actually grab the webpages (through HTTP - you can use wget or write another php script that just uses file() with URLs) and save them to the public website locations when you are "done".一种方法是像往常一样在 PHP 中创建站点,并让脚本实际抓取网页(通过 HTTP - 您可以使用 wget 或编写另一个 php 脚本并将它们保存到仅使用文件的脚本) “完成”后的公共网站位置。 Then you can just run the script again when you decide to change the pages again.然后,当您决定再次更改页面时,您可以再次运行脚本。 This method is quite useful when you have a slowly changing database and lots of traffic, as you can eliminate all SQL queries on the live site.当您有一个缓慢变化的数据库和大量流量时,此方法非常有用,因为您可以消除实时站点上的所有 SQL 查询。

If you use modx it has a built in function to export static files.如果你使用 modx 它有一个内置的 function 来导出 static 文件。

If you have a number of pages, with all sorts of request variables and whatnot, probably one of the spidering tools the other commenters have mentioned (wget, lwp-mirror, etc) would be the easiest and most robust solution.如果您有许多页面,包含各种请求变量等等,那么其他评论者提到的爬虫工具之一(wget、lwp-mirror 等)可能是最简单和最强大的解决方案。

However, if the number of pages you need to get is low, or at least manageable , you've got a few options which don't require any third party tools (not that you should discount them JUSt because they are third party).但是,如果您需要获取的页面数量很少,或者至少是可管理的,那么您有一些不需要任何第三方工具的选项(并不是因为它们是第三方就应该打折它们)。

  1. You can use php on the command line to get it to output directly into a file.您可以在命令行上使用 php 将其直接放入 output 文件中。

    php myFile.php > myFile.html

    Using this method could get painful (though you could put it all into a shell script), and it doesn't allow you to pass variables in the same way (eg: php myFile.php?abc=1 won't work).使用这种方法可能会很痛苦(尽管您可以将其全部放入 shell 脚本中),并且它不允许您以相同的方式传递变量(例如: php myFile.php?abc=1 work).

  2. You could use another PHP file as a "build" script which contains a list of all the URLs you want and then grabs them via file_get_contents() or file() and writes them to a local file.您可以使用另一个 PHP 文件作为“构建”脚本,其中包含您想要的所有 URL 的列表,然后通过file_get_contents()file()它们并将它们写入本地文件。 Using this method, you can also get it to check if the file has changed ( md5_file() should work for that), so you'll know what to give your client, should they only want updates.使用这种方法,您还可以使用它来检查文件是否已更改( md5_file()应该适用于此),因此如果他们只想要更新,您将知道给您的客户什么。

  3. Further to #2, before you write the output to file, scan it for local urls and then add those to your list of files to download.继 #2 之后,在将 output 写入文件之前,扫描它以查找本地 url,然后将它们添加到要下载的文件列表中。 While you're there, change those urls to link to what you'll eventually name your output so you have a functioning web at the end.当您在那里时,将这些网址更改为链接到您最终将命名为 output 的内容,这样您最后就有了一个正常工作的 web。 Note of caution here - if this is sounding good, you could probably use one of the tools which already exist and do this for you.请注意 - 如果这听起来不错,您可能可以使用已经存在的工具之一并为您执行此操作。

Alternatively to wget you could use (Win|Web)HTTrack ( Website ) to grab the static page.或者 wget 您可以使用 (Win|Web)HTTrack ( Website ) 来获取 static 页面。 HTTrack even corrects links to files and documents to match the static output. HTTrack 甚至更正文件和文档的链接以匹配 static output。

You can use python or visual basic (or your choice) to create your static files all at once then upload them.您可以使用 python 或 Visual Basic(或您的选择)一次创建您的 static 文件,然后上传它们。

For a project with 11 million business listings in excel files I used VBA to extract the spreadsheet data into 11 mil small.php files, then zipped, ftp'd, unzipped.对于在 excel 文件中包含 1100 万个企业列表的项目,我使用 VBA 将电子表格数据提取到 11 百万个小文件中。php 文件,解压缩然后压缩。

https://contactlookup.us https://contactlookup.us

Voila - a super fast business directory瞧——一个超级快速的商业目录

I started with Jekyll, but after about half million entries the generator got bogged down.我从 Jekyll 开始,但在大约 50 万次输入后,生成器陷入了困境。 For 11 million it looked like it would finalize the build in about 2 months!对于 1100 万,看起来它将在大约 2 个月内完成构建!

I do it on my own web site for certain pages that are guaranteed not to change -- I simply run a shell script that could be boiled to (warning: bash pseudocode):我在我自己的 web 站点上针对某些保证不会更改的页面执行此操作——我只是运行一个 shell 脚本,该脚本可以被煮沸(警告:ZD574D4BB40C84861791A694A999CCE9):

find site_folder -name \*.static.php -print -exec Staticize {} \;

with Staticize being:静态化为:

# This replaces .static.php with .html
TARGET_NAME="`dirname "$1"`/"`basename "$1" .static.php`".html
php "$1" > "$TARGET_NAME"

wget is probably the most complete method. wget 可能是最完整的方法。 If you don't have access to that, and you have a template based layout, you may want to look into using Savant 3. I recommend Savant 3 highly over other template systems like Smarty.如果您无权访问它,并且您有基于模板的布局,则可能需要考虑使用 Savant 3。我强烈推荐 Savant 3,而不是 Smarty 等其他模板系统。

Savant is very light weight and uses PHP as the template language, not some proprietary sublanguage. Savant 重量很轻,使用 PHP 作为模板语言,而不是一些专有的子语言。 The command you would want to look up is fetch(), which will "compile" your template and place it in a variable that you can output.您要查找的命令是 fetch(),它将“编译”您的模板并将其放在您可以 output 的变量中。

http://www.phpsavant.com/ http://www.phpsavant.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM