.


web tutorials


 

Including or inserting php file output into an html file

I looked far and wide for a solution to this conundrum. I have a site whose main entry page is plain html, and for search-engine stability and to preserve bookmarks, I want to keep it that way. However, I tired of using FeedSplitter, which can be a bit slow and has been criticized for security vulnerabilities; I prefer a php-based RSS feed interpreter, but you need php to use it.

There are many other reasons to integrate the results of a php file into an html file. But here is how you can do it.

Important note: this illustration assumes a user named "weborial" whose web-visible files are in /home/weborial/public_html.

  1. Set up the php file.
  2. Create a batch file in the non-public area of your server (e.g. /home/weborial rather than /home/weborial/public_html)
  3. The batch file should contain a call to your php file (e.g. php /home/weborial/test.php) and have permissions of 755. In our example, we'll call it test.bat
  4. Set up a crontab entry like: 5 * * * * /home/weborial/test.bat > /home/weborial/public_html/test.inc — that will run the program every five minutes, putting the results into a file named test.inc
  5. Now, you can simply add this SSI code into your html file (assuming you have includes set up): <!--#include virtual="/test.inc"--> (or, depending on your setup, <!--#include file="/test.inc"-->)
  6. If your server requires files to be named ".shtml" rather than ".html" to have server-side includes, add this line to your .htaccess file (which you may need to create in your public folder, e.g. public_html/.htaccess): AddHandler server-parsed html
  7. If you're getting an unwanted header line at the top of your text file (test.inc), telling what kind out output it is, add a second line to your test.php file, which will delete a single line from the text file just after it’s generated: sed -i '1 d' /home/weborial/public_html/test.inc

This is especially handy if you're using a cacheing RSS feed translation program like SimplePie; you can set up SimplePie, set up a test.php program that essentially contains your SimplePie code (see sample below), and then put it into your html with the above contraption.

Sample SimplePie test.php file to pull the first three headlines (and just the headlines) from a fictional WordPress RSS feed:

<?php
require_once('/home/weborial/public_html/simplepie/simplepie.inc');
$feed = new SimplePie('http://www.weborial.com/news/feed/');
$feed->handle_content_type();
$feed->set_cache_location('/home/weborial/sp-cache');
$max = $feed->get_item_quantity(3);
for ($x = 0; $x < $max; $x++):
$item = $feed->get_item($x);
?>
<p><b><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></b></p>
<?php endfor; ?>