2

How do you programmatically convert a dynamic PHP file into a static HTML file, that would obviously have all dynamic PHP-related values baked in as static HTML?

Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
  • 1
    Copy the output, the HTML source of the generated web page! Maybe you mean *how to do this automatically at runtime*? – lorenzo-s Dec 20 '11 at 16:00
  • Sounds like you are looking for caching information, check here: http://www.slideshare.net/anisniit/caching-new – Jim Dec 20 '11 at 16:02

7 Answers7

10

As the beginning of your script place this:

<?php
    ob_start();
?>

At the very end of the script, place this:

<?php
    $myStaticHtml = ob_get_clean();
    // Now you have your static page in $myStaticHtml
?>

Output buffering reference here:

http://php.net/manual/en/book.outcontrol.php
http://www.php.net/manual/en/function.ob-start.php
http://www.php.net/manual/en/function.ob-end-clean.php

lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
2

Somewhere on the top of your PHP file:

ob_start();

After all the processing:

$output = ob_get_clean();
file_put_contents('filename', $output);

And if you then also want to output it for that process (for instance if you want to write cache on runtime but also show that page to that user:

echo $output;
stefandoorn
  • 1,032
  • 8
  • 12
1

View the HTML source in a browser, and save that.

If you want to do this automatically, then use output buffering.

Jim
  • 18,673
  • 5
  • 49
  • 65
pgl
  • 7,551
  • 2
  • 23
  • 31
1
<?php

ob_start(); // start output buffering

echo "your html and other PHP"; // write to output buffer

file_put_contents("file.html", ob_get_contents()); // write the contents of the buffer to file

ob_end_clean(); // clear the buffer
Prisoner
  • 27,391
  • 11
  • 73
  • 102
1

You can also do it with wget

For example:

$ wget -rp -nH --cut-dirs=1 -e robots=off http://www.domain.com/
macjohn
  • 1,755
  • 14
  • 18
0

The easiest way is to open the page and to copy "view source"
You can also use
php function $homepage = file_get_contents('http://www.example.com/');

and save it in a file

lvil
  • 4,326
  • 9
  • 48
  • 76
0

From the related posts:

<?php  
  job_start();  // your PHP / HTML code here  
  file_put_contents('where/to/save/generated.html', ob_get_clean());  
?> 
user408041
  • 1,064
  • 1
  • 8
  • 12