PHP - How to programmatically bake out static HTML file? - php

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?

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

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;

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

View the HTML source in a browser, and save that.
If you want to do this automatically, then use output buffering.

You can also do it with wget
For example:
$ wget -rp -nH --cut-dirs=1 -e robots=off http://www.domain.com/

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

From the related posts:
<?php
job_start(); // your PHP / HTML code here
file_put_contents('where/to/save/generated.html', ob_get_clean());
?>

Related

How to save php file as csv file?

script1.php returns results:
helen,hunt
jessica,alba
script2.php returns results:
bradley,cooper
brad,pitt
script.php looks like this
<?php
echo "name,surname";
require_once('script1.php');
require_once('script2.php');
?>
and will return
name,surname
helen,hunt
jessica,alba
bradley,cooper
brad,pitt
Question
Maybe this is obvious to someone, but I am struggling for a while how to save this php file: script.php as csv file? script1.php and script2.php both produce result over while loop, so I would be able to save results as array inside of a while loop however hopefully someone will offer an easy solution to my original question.
If I get it right, you are trying to save the output of those scripts to a CSV file.
Try doing:
ob_start();
echo "name,surname";
require_once('script1.php');
require_once('script2.php');
$result = ob_get_contents();
file_put_contents('filename.csv', $result)
You can also take a look at fputcsv: http://php.net/manual/en/function.fputcsv.php
If your question is how to do it having the access to the terminal, then just execute it
php -f script.php > file.csv
If you want to do it from the script, you can use output buffering. It will be along the lines of:
<?php
ob_start();
echo "name,surname";
require_once('script1.php');
require_once('script2.php');
$size=ob_get_length();
if ($size > 0)
{
$content = ob_get_contents();
// save $content to file here
ob_clean();
}
?>
Or, alternatively, if you can change script1.php and script2.php, make them open the file for appending (fopen('fname.csv', 'a')), and write to that file, it will result in rows from both of them written to the file without overwriting each other.

Store PHP Output To File?

I'm working on a cron php script which will run once a day. Because it runs this way, the output from the file can't be seen.
I could literally write all the messages I want into a variable, appending constantly information I want to be written to file, but this would be very tedious and I have a hunch not necessary.
Is there a PHP command to tell the write buffer to write to a log file somewhere? Is there a way to get access to what has been sent to the buffer already so that I can see the messages my script makes.
For example lets say the script says
PHP:
<?
echo 'hello there';
echo 'hello world';
?>
It should output to a file saying: 'hello therehello world';
Any ideas? Is this possible?
I'm already aware of
file_put_contents('log.txt', 'some data', FILE_APPEND);
This is dependent upon 'some data', when I don't know what 'some data' is unless I put it in a variable. I'm trying to catch the results of whatever PHP has outputted.
You may want to redirect your output in crontab:
php /path/to/php/file.php >> log.txt
Or use PHP with, for example, file_put_contents():
file_put_contents('log.txt', 'some data', FILE_APPEND);
If you want to capture all PHP output, then use ob_ function, like:
ob_start();
/*
We're doing stuff..
stuff
...
and again
*/
$content = ob_get_contents();
ob_end_clean(); //here, output is cleaned. You may want to flush it with ob_end_flush()
file_put_contents('log.txt', $content, FILE_APPEND);
you can use ob_start() to store script output into buffer. See php documentation ob_get_clean
<?php
ob_start();
echo "Hello World";
$out = ob_get_clean();
$out = strtolower($out);
var_dump($out);
?>
If You're using cron I suppose that You run this on a Unix machine so:
One of approach is to write everything You want to stdout stream so in Unix You may grab this output to a file:
in php script:
$handle = fopen("php://stdout","w");
fwrite($handle,"Hello world"); // Hello world will be written to console
in cron job grab this output to a file:
#hourly php /var/www/phpscript.php >> /path/to/your/outputfile.txt
Notice: >> operator will append to a file and > operator will overwrite file by new data. File will be created automatically by first write
So everything you put to fwrite call as second argument will be placed in /path/to/your/outputfile.txt
You may call fwrite as many time as you want. Don't forget to close handler by fclose($handle);

Return html response from PHP page in to variable

I am trying to generate an email with some HTML that is created via another PHP file.
Email generating file is run by a cron running every hour.
Another file exists that generates the HTML required for the email.
The HTML generating file does not have a function that I can call, for instance:
$emailBody = generateHTML($id);
The HTML generating file was designed to be included on a page that you wished to display the HTML, for instance:
include "htmlGenerator.php";
My question is: How can I get what the htmlgenerator.php file returns in to a variable, ready to be pushed in to an email.
Apologies if this is not very clear, I will be happy to answer any questions.
Thanks in advance!
If I understood what you said, you can use output buffering,
so that you can get the output of htmlGenerator.php
For example:
ob_start();
// as an example
echo "Hello World";
// or in our case
include "htmlGenerator.php";
$result = ob_get_contents();
ob_end_clean();
You can also create a simple function like this:
function include_output($filename)
{
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
$mycontent = include_output("htmlGenerator.php");
Read the php documentation for further details.
Check out the built-in functions ob_start and ob_get_clean.
You can then do something like:
ob_start();
include( "file.php");
$var = ob_get_clean();

How to assign the contents of a file to a variable in PHP

I have a document file containing HTML markup. I want to assign the contents of the entire file to a PHP variable.
I have this line of code:
$body = include('email_template.php');
When I do a var_dump() I get string(1) "'"
Is it possible to assign the contents of a file to a variable?
[Note: the reason for doing this is that I want to separate the body segment of a mail message from the mailer script -- sort of like a template so the user just modifies the HTML markup and does not need to be concerned with my mailer script. So I am including the file as the entire body segment on mail($to, $subject, $body, $headers, $return_path);
Thanks.
If there is PHP code that needs to be executed, you do indeed need to use include. However, include will not return the output from the file; it will be emitted to the browser. You need to use a PHP feature called output buffering: this captures all the output sent by a script. You can then access and use this data:
ob_start(); // start capturing output
include('email_template.php'); // execute the file
$content = ob_get_contents(); // get the contents from the buffer
ob_end_clean(); // stop buffering and discard contents
You should be using file_get_contents():
$body1 = file_get_contents('email_template.php');
include is including and executing email_template.php in your current file, and storing the return value of include() to $body1.
If you need to execute PHP code inside the of file, you can make use of output control:
ob_start();
include 'email_template.php';
$body1 = ob_get_clean();
file_get_contents()
$file = file_get_contents('email_template.php');
Or, if you're insane:
ob_start();
include('email_template.php');
$file = ob_end_flush();
As others have posted, use file_get_contents if that file doesn't need to be executed in any way.
Alternatively you can make your include return the output with the return statement.
If your include does processing and outputs with echo [ed: or leaving PHP parsing mode] statements you can also buffer the output.
ob_start();
include('email_template.php');
$body1 = ob_get_clean();
TimCooper beat me to it. :P
Try using PHP's file_get_contents() function.
See more here: http://php.net/manual/en/function.file-get-contents.php
Yes you can its easy.
In the file you want to use the variable place this
require_once ‘/myfile.php';
if(isset($responseBody)) {
echo $responseBody;
unset($responseBody);
}
In the file you are calling /myfile.php place this
$responseBody = 'Hello world, I am a genius';
Thanks
Daniel
You have two optional choices
[Option 1]
Create a file called 'email_template.php'
inside the file add a variable like so
$body = '<html>email content here</html>';
in another file require_once 'email_template.php'
then echo $body;
[Option 2]
$body = require_once 'email_template.php';

Capture STDOUT from included file to variable

How to capture everything what leaves included file to variable or another file.
Output buffering is the way to go.
<?php
ob_start(); // Start buffering output
include '/path/to/file/';
$myVariable = ob_get_clean(); // Put the buffered output
// into $myVariable and clear
// the output buffer
http://www.php.net/manual/en/function.ob-start.php
http://www.php.net/manual/en/function.ob-get-clean.php
Using output buffers: http://www.php.net/manual/en/function.ob-get-contents.php

Categories