HomePHP Page 4 - Rockin’ RSS with PHP on your HTML
Starting with the header - PHP
An RSS feed lets webmasters tease visitors into returning to their websites again and again to check out new content. Danny Wall explains how to set up this simple, automated, spamless way of getting Web surfers to come back for more.
In fact, using the magic bag of tricks known as PHP, your own RSS file can be automatically created with very little effort, and once the PHP script is finished, the whole thing is “maintenance/management free.”
To provide a little further clarification, we’re going to cover the basics of RSS file creation (indeed, the example file I gave you above is only a basic RSS document). In future articles we’ll talk about other elements to the RSS specification, what they mean, and why you might want to use them (and I’ll provide you the additions to the PHP script you’ll get in this article to add those elements).
For nearly every website, some of the information in the RSS file--lets call it the “header” info--will be the same/static each time the file is created. That info is:
<?xml version='1.0' ?> <rss version='2.0'> <channel> <title>Wolf Data News</title> <link>http://www.wolfdatasystems.com/</link> <description>News for programmers, business, and home computer users.</description> <language>en-us</language> <docs>http://www.wolfdatasystems.com/rss.xml</docs>
As you can see, this amounts to the very first group of information in the file; and obviously you’ll want your own titles, your own links, and your own “feed description,” but the above will almost certainly be static information in your feed. Every day when my site updates the RSS file, this information is unchanged.
Now, lets go into building the PHP script to create the RSS file. For the sake of discussion, lets call this file create_rss.php:
<?php // open a file pointer to an RSS file $fp = fopen ("rss.xml", "w");
// Now write the header information fwrite ($fp, "<?xml version='1.0' ?><rss version='2.0'><channel>”);
So far, we haven’t done anything too challenging. All we’ve done is opened a file, and written the header information that won’t be changing from file creation to file creation.
As you can see, I have broken the header file writes into several pieces. I’ve done this simply to make the script easier to see instead of having stuff trailing off to the right side of notepad forever. It isn’t at all necessary, but I do recommend it simply to make it easier to modify the file in the future.