Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Rockin’ RSS with PHP on your HTML
  2. An idea that's catching on
  3. How--and why--to get started
  4. Starting with the header
  5. Adding content
  6. Tease the reader
By: Danny Wall
Rating: starstarstarstarstar / 42
November 23, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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

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

fwrite ($fp, "<link>http://www.wolfdatasystems.com/</link>");

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

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

fwrite ($fp, "<docs>http://www.wolfdatasystems.com/rss.xml</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.



 
 
>>> More PHP Articles          >>> More By Danny Wall
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: