How can I create a site in php and have it generate a static version?
Asked Answered
S

11

23

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?

Update: Thanks to everyone who suggested 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.

Swats answered 15/9, 2008 at 20:28 Comment(1)
This is exactly what my static site generator does with vanilla PHP and an integrated GitHub Pages support at github.com/atas/ssgHolladay
P
47

If you have a Linux system available to you use wget:

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

Options

  • -k : convert links to relative
  • -K : keep an original versions of files without the conversions made by wget
  • -E : rename html files to .html (if they don’t already have an htm(l) extension)
  • -r : recursive… of course we want to make a recursive copy
  • -l 10 : the maximum level of recursion. if you have a really big website you may need to put a higher number, but 10 levels should be enough.
  • -p : download all necessary files for each page (css, js, images)
  • -N : Turn on time-stamping.
  • -F : When input is read from a file, force it to be treated as an HTML file.
  • -nH : By default, wget put files in a directory named after the site’s hostname. This will disabled creating of those hostname directories and put everything in the current directory.

Source: Jean-Pascal Houde's weblog

Pensionary answered 15/9, 2008 at 20:34 Comment(3)
With apache and RewriteRule you can still find the old pages without html extension (as example.com/hello which is converted in example.com/hello.html). For example "RewriteRule ^hello$ /hello.html [PT]"Seashore
Check here for ideas on how to add the .html extension to all incoming requests: #5745990Playwriting
This solution doesn't seem to wait for ajax requests to finish, so it doesn't see any ajax-loaded content. That's a major issue considering most of the content of my site is loaded dynamically from a database.Coalition
P
4

build your site, then use a mirroring tool like wget or lwp-mirror to grab a static copy

Pooka answered 15/9, 2008 at 20:29 Comment(0)
A
3

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. 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. 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.

Aroid answered 15/9, 2008 at 20:39 Comment(0)
S
1

Create the site as normal, then use spidering software to generate a HTML copy.

HTTrack is software I have used before.

Schoenberg answered 15/9, 2008 at 20:29 Comment(0)
M
1

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". 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.

Mohammedmohammedan answered 15/9, 2008 at 20:31 Comment(0)
Y
1

If you use modx it has a built in function to export static files.

Yellows answered 15/9, 2008 at 20:42 Comment(0)
D
1

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.

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 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).

  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. 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.

  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. 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. 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.

Dispread answered 15/9, 2008 at 20:48 Comment(0)
C
1

Alternatively to wget you could use (Win|Web)HTTrack (Website) to grab the static page. HTTrack even corrects links to files and documents to match the static output.

Cradle answered 15/9, 2008 at 22:52 Comment(0)
D
1

You can use python or visual basic (or your choice) to create your static files all at once then upload them.

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.

https://contactlookup.us

Voila - a super fast business directory

I started with Jekyll, but after about half million entries the generator got bogged down. For 11 million it looked like it would finalize the build in about 2 months!

Deragon answered 15/7, 2022 at 1:24 Comment(0)
R
0

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):

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"
Responsible answered 15/9, 2008 at 20:32 Comment(0)
H
0

wget is probably the most complete method. 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 is very light weight and uses PHP as the template language, not some proprietary sublanguage. 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.

http://www.phpsavant.com/

Howe answered 15/9, 2008 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.