I have the following scenario.
I have a PHP website witch contains about 3 webpages with dynamic content. I want to render this to static content to another file. Eg. contact.php -> contact.html
My code looks like this
ob_start();
$content = require_once 'contact.php';
ob_end_flush();
file_put_contents("contact.html", $content);
Somehow this is not working ;-?(
require_once() doesn't return the content outputted by a script. You need to get the script output that is stored in the output buffer:
ob_start();
require_once('contact.php');
$content = ob_get_clean();
file_put_contents('contact.html', $content);
ob_get_clean():
Gets the current buffer contents and delete current output buffer.
ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean().
ob_start();
require_once('contact.php');
$content = ob_get_contents();
ob_end_clean();
file_put_contents("contact.html", $content);
require_once
opens the file and tries to parse it as PHP. It won't return its output. What you're probably looking for is this:
<?php
ob_start();
require_once('file.php');
$content = ob_get_contents();
ob_end_flush();
// etc...
?>
This way, the script both stores the data in $content AND dumps them to standard output. If you only wanted $content to be populated, use ob_end_clean()
instead of ob_end_flush()
.
Checkout ob_get_flush( http://www.php.net/manual/en/function.ob-get-flush.php )
Basically try to do
if(!is_file("contact.html")){
ob_start();
require_once 'contact.php';
$content = ob_get_flush();
file_put_contents("contact.html", $content);
}else{
echo file_gut_contents("contact.html");
}
This should buffer the the output from contact.php
and dump it when needed.