Reputation: 581
I want to make a cms which generates html pages and creates files to store each page.
Ideally i need something like that:
<?php
$file1 = get_produced_html_from('mainPage.php');
/* write the file to a directory*/
$file2 = get_produced_html_from('ProductsPage.php');
/* write the file to a directory*/
?>
Is there any function i missed instead of require, include, require_once, include_onse etc?
To clarify: I don't need the php code inside the .php file. I need the html content only which means that the php file should be executed first.
Do you believe the solution is something like using http://php.net/manual/en/function.file-get-contents.php by reading http://domain.com/templates/mainPage.php as a html stream?
Thank you a lot.
Upvotes: 0
Views: 2734
Reputation: 25698
You need to catch the output from the buffer.
Here is a piece of code I wrote for somebody to demonstrate a very simple view renderer class.
public function render($file) {
$file = $this->viewPath = APP_ROOT . 'View' . DS . $file . '.php';
if (is_file($file)) {
ob_start();
extract($this->_vars);
include($file);
$content = ob_get_contents();
ob_end_clean();
} else {
throw new RuntimeException(sprintf('Cant find view file %s!', $file));
}
return $content;
}
It turns on the output buffer (ob_start()) executes the php file and sets the variables and then gets the buffer (ob_get_contents()) and then it cleans the buffer for the next operation (ob_end_clean()). You can also use ob_end_flush() to directly clean and send the buffer. I would not do that and instead do a proper shutdown process of the app and ensure everything is done and was done right without errors before sending the page to the client.
I think I'm going to make the whole code available on Github soon. I'll update the answer then.
Upvotes: 3
Reputation: 14863
You can just use cURL to get the whole rendered output from an url.
You can use it like this:
// Initiate the curl session
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/mypage.php');
// Allow the headers
curl_setopt($ch, CURLOPT_HEADER, true);
// Return the output instead of displaying it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the curl session
$output = curl_exec($ch);
// Close the curl session
curl_close($ch);
Upvotes: 0