Rockin’ RSS with PHP on your HTML

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.

While you may have a good grasp of RSS and what it is, stick with me for just a second and after we run over some preliminary stuff, we’ll move into creating an RSS feed of your own, for your website, using PHP.

RSS (which stands for Really Simple Syndication) is, at its core, a push technology that was developed by Netscape. It gives webmasters the ability to do two things: the first is to easily add content from other websites to their own, and it also easily allows webmasters to push their own unique content out to other people and other websites.

The use that will probably be the most exciting to you is that people who are sick and tired of the mountain of spam filling their inboxes, and tired of having to surf to dozens of sites to get the information they want, are turning to RSS–or more specifically an RSS reader, to obtain news and information on the latest updates to the blogs they follow.  An RSS reader (such as the one that can be found at allows people to see all of the sites they follow in one convenient place and quickly and easily see if there’s anything new–and even what is new.

You can almost think of it as turning the Web into an email application, but without all the spam–because it lets you see which sites have new information in almost the exact same way you know if you’ve read an email or not.

This means that you can add new content to your site, and all of your visitors will know the content has been added and will know what was added. It also means that your own site content can be syndicated across the Web, driving traffic from a huge number of sites directly into your own.

{mospagebreak title=An idea that’s catching on}

This concept is so powerful that it is now being picked up and used by the largest players in the Internet news space such as Yahoo!News. It is also used by many blogging services such as Google’s recently acquired Blogger (actually, it uses Atom, but it is essentially the same thing) to keep their loyal readers up to date with new additions to the blogs. 

In other words, the format is growing in leaps and bounds, driven not only by the biggest sites on the Web, but also by the cultural wildfire known as the blogosphere.

In the old days, when a Web site had a new update they wanted people to know about, it meant sending out an email, or using an email newsletter. The problem is that in today’s day and age, with many people getting hundreds of spam messages a day, people are block deleting much of their email–including messages they actually asked to receive. 

Now, with RSS, it is possible to simply update your website, and the RSS readers that people have installed will automatically tell them an update has happened, what the update was, and give them the ability to quickly and easily surf to your site. No more email, no more worrying if the email you sent even reached the person.

Further, all of that doesn’t even count the ability for other webmasters to add your content to their site with no muss and no fuss (again, syndicating your content across the web).

{mospagebreak title=How–and why–to get started}

You should know that there are a lot of tutorials and the like on how to add RSS content to your own site.  An excellent one can be found at:

Additionally, there are a wide range of tools to make adding RSS to your site easy with hardly writing any code of you own. I personally use the MagpieRSS toolset created in PHP which can be found at So instead of covering how to get RSS added to your site, what I’d like to do is talk about how you can build your own RSS feed.

Some webmasters that I know groan over the thought of RSS. It’s yet another new technology to learn, another new thing to add to their site and manage. The thing is, RSS is really nothing more than an XML application, and if done right, it doesn’t need any management.  Everything can be (indeed, should be) automated. 

In fact, RSS it is the first widespread use of XML. It is the first time XML is being used to do something useful for the average Web surfer AND for the average webmaster.

To start with, the easiest thing to do will be to take a look at what an RSS file looks like. You can see an example RSS file at:

To anyone with any knowledge of HTML it isn’t really hard to figure out what is going on.  There isn’t anything strange or scary, there isn’t anything to make you cringe.

{mospagebreak title=Starting with the header}

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′>
<title>Wolf Data News</title>
<description>News for programmers, business, and home computer users.</description>

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: 

// 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>”);

fwrite ($fp, “<title>Wolf Data News</title>”);

fwrite ($fp, “<link></link>”);

fwrite ($fp, “<description>News for programmers, business, and home computer users.</description>”);

fwrite ($fp, “<language>en-us</language>”);

fwrite ($fp, “<docs></docs>”);

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.

{mospagebreak title=Adding content}

At this point, you now need to begin adding the “item” level information; which is simply the content that has been added to your site. Before we get started on this, there are some things you need to know.

Generally, an RSS feed contains not only the most recently added content to your site, but quite a bit of “back” content as well. This is to allow any new users to not only see the most recent additions, but also see what has been done before they picked up your feed. This way, they’ll easily be able to look at that as well.

It’s a good thing for the new user, and for you, as often a person that picks up a new feed will spend some time checking out a lot of the “past” content and catching up on what you’ve got going.

That means we need to do two things.

First, you need to database your new content. In otherwords, you should be adding new content to your database, and your website should be pulling that new content out of the database. This will allow you to utilize PHP with maximum benefit and speed to build the RSS file.

$db = mysql_connect(“localhost”,”your_db_uid”,”your_db_pw”);
$content_sql = “select * from content where page=’index’ order by id desc”;

Obviously in the above line, you are seeing how I do it. Your query may be different depending on how you are databasing your content.

$content_result = mysql_query($content_sql);

So far, we’re still in the “basic” neck of the woods.  In fact, all we need to do now is pull the content out of your site and put it into the RSS file.

while ($content_rec = mysql_fetch_row($content_result)) {
    fwrite ($pd, “<item>”);

    $headline = $content_rec[0];
    $content_1 = substr($content_rec[1], 0, 250);
    $content = strip_tags($content_1);
    if (strlen($content_rec[1]) > 250) {
        $content = $content . “….”;
    fwrite ($fp, “<title>$headline</title>”);
    fwrite ($fp, “<description>$content</description>”);
    $item_link = “$content_rec[3]“;
    fwrite ($fp, “<link>$item_link</link>”);

    fwrite ($fp, “</item>”);

{mospagebreak title=Tease the reader}

And essentially that’s it. Notice how I did NOT put the entire content into the RSS feed. You can put the entire content into the file if you want. Personally, I don’t recommend doing this. Instead, put just enough to tease the reader so that they want to visit your site.

Now, you will note that I do move the data around just a bit to not only shorten the length of what I’m sending out on the feed, but also to add the ellipses to let readers know there is more.

And again, how you decide to database your content will affect exactly how you pull that content out.

Anyway, at this point, all we need to do is close off the file.

fwrite ($fd, “</channel></rss>”);
fclose ($fd);


It is a simple enough process.  You can now have that ran by a cron job, or simply run the php file from a browser once a day.

Again, if you are running some sort of a content management system (and I’ll talk about how to easily build your own in future articles), creating the RSS file is obviously a simple matter of writing the static “header” information for your site/feed, pulling the content out of the database, writing each item of new content between “<item>” tags, and then closing the file off with the ending tags.

You may want to add in some other information into this feed, and again, we’ll also talk about what other information there is to add, and why you may want to put that into the feed. We’ll also talk about, if you are adding content via RSS feeds to your site, how to make THAT content from that OTHER feed a part of your own RSS feed. This will ensure that you’ve got a constantly updating RSS feed so that people will want to keep coming back to your site, driving up traffic, unique visitors per day, and page views per visitor.

Google+ Comments

Google+ Comments