PHP - How to programmatically bake a static HTML file?

How do you programmatically convert a dynamic PHP file into a static HTML file, which will obviously have all the dynamic values ​​associated with PHP that are baked as static HTML?

+2
source share
7 answers

As a start to your script, put this:

<?php
    ob_start();
?>

At the very end of the script, put this:

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

Output buffering 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

+9
source

Browse the HTML source in the browser and save it.

, .

+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
+1

- PHP :

ob_start();

:

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

And if you also want to output it for this process (for example, if you want to write cache at runtime, but also show this page to this user:

echo $output;
+1
source

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

and save it to a file

0
source

From related posts:

<?php  
  job_start();  // your PHP / HTML code here  
  file_put_contents('where/to/save/generated.html', ob_get_clean());  
?> 
0
source

You can also do this with wget

For instance:

$ wget -rp -nH --cut-dirs=1 -e robots=off http://www.domain.com/
0
source

All Articles