Create Custom Username RSS Feed Using PHP

Sep 19, 2012
HTML5 PHP
By

So, you built a website with an amazing strength of potential for millions of internet aficionados to venture, read information, blog, whatever –and maybe make you a few pesos in the big scheme of things. There is a simple, yet powerful tool to gain traffic through: RSS.

RSS means Really Simple Syndication. To syndicate just means gather all the information in once place, where people can subscribe via their web browser, to an article list. But, the words “really simple” on the programming side, are not so simple –as you might have guessed. Creating an RSS Feed, should be quite easy. And after racking my brain for days over the programming connection between my existing PHP website and testing out several Feed Generators { like Mag Pie or Simply Smarty } had a moment where my iMac nearly plunged out the window.

Luckily, I remained calm and went back to my old school PHP programming know-how for the answer to Customizing An RSS Feed without bloated parsers. The customization of this type of Feed creates a Username based RSS Channel in PHP, that returns valid. For each user generated page, you can place the link to that specific group of Articles on the website. The system automatically creates the files needed with each registration. This means less work for you, more flexibility, certainly no manual entry – and if all goes well, increased subscribers.

Creating the entire Custom RSS Feed will take you minutes, literally. From here you can allow Members to jack into Feedburner, Pheedo or any other Syndication they prefer, which can also be used for Internal Tax and Folks -onomy. You will need three {3} items in all:

  1. A Make File function at Member Registration
  2. The PHP-XML Hybrid File { template }
  3. The Output Script/Feed Link

1. A Make File function at Member Registration

On the Registration Confirmation page call a MAKE FILE function. This tells the server to create a file titled Username.rss.php.  It is vital you make this file, else the RSS Output will not be created. The function gets the POST Username and tells the server to MAKE FILE called Username.rss.php.

$USERNAME=$_POST['USERNAME'];
$FILE=$USERNAME.'.rss.php';
if(!$handle=fopen($FILE, 'a')) {
echo "Crickets, cannot open file ($FILE)";
exit
}
$ginger=file_get_contents("rss.php");

2. The PHP-XML Hybrid File { template }

Now, here is the tricky part: you will need to GET the base RSS-PHP hybrid Template. Why? This saves a lot of time and headache. The function gets all the Template information and appends it to the new file called Username.rss.php. This will include the RSS Elements for each Member who joins. Write the header to the new RSS file and move it to the proper directory. { You may choose whatever folder you like, just so long as you remember where you created it so you may link it later on }. The above code now automatically creates the RSS-PHP file in that directory. In order for that file to be created, you need the base code or Hybrid Template, and obviously a connection to the database.

3. The Output Script/Feed Link

One of the unique features of RSS, it is written in XML. Essentially, you can customize the items like Title, Main URL, Main Photo and Child Elements as well. Some browsers will enable photos for each feed listing, some do not. Feedburner and Pheedo definitely do, by creating an XHTML (XML+HTM) page for the feed. This is great for putting in Ads.

The issue with RSS/XML and PHP is simply the language. Communication between is like speaking Russian to a Dolphin, neither understand a word the other is saying. However, there is a way to unite the languages and everyone go home happy. This is called parsing. One language meets another and information is translated. In fact, the parser IS the translator. To make this work, we tell the server this PHP File, should be read as an XML file. This is found in the Content Type. Most web pages tell you in the header what type of content is designed or allowed for the page. If you are using XHTML Transitional or even Strict, you can display pretty much any code. With PHP/XML you need to specifically assign which Content Type. In this case, XML/RSS. Let’s create the Main RSS Template file first and customize it.

header(Content-Type: application/rss+xml;
charset=ISO-8859-1);
require_once('mydataconnect.php');

$USERNAME= basename($_SERVER['SCRIPT_FILENAME']);
$USERNAME= str_replace('_',' ',$USERNAME);
$USERNAME= str_replace('.rss.php','',$USERNAME);
$USERNAME= strtolower($USERNAME);
$USERNAME= ucfirst($USERNAME);

$feed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$feed .= '<rss version="2.0">';
$feed .= '<channel>';
$feed .= '<title>Your RSS</title>';
$feed .= '<link>http://www.yourwebsite.com</link>';
$feed .= '<description>Describe Website</description>';
$feed .= '<language>en-us</language>';
$feed .= '<copyright>yourwebsite.com</copyright>';

$query="SELECT * FROM RSS WHERE USERNAME='$USERNAME'
ORDER BY PUBLISHED DESC LIMIT 15";
$result = mysql_query($query) or die();

while($row = mysql_fetch_array($result))
{ extract($row);
$LINK ="http://yourwebsite.com/$Username/$TITLE.php";
$PHOTO ="http://yourwebsite.com/$Username/$PHOTO1";
$DESCRIPT = $row['DESCRIPTION'];

$feed .= '<item>';
$feed .='<image>';
$feed .='<url>$PHOTO</url>';
$feed .='<title>YOUR RSS</title>';
$feed .='<link>$USERNAME.rss.php</link>';
$feed .='<width>50px</width>';
$feed .='<height>50px</height>';
$feed .='</image>';
  $feed .= '<title>$TITLE</title>';
  $feed .= '<description>$DESCRIPT</description>';
  $feed .= '<link>$LINK</link>';
$feed .= '</item>';
}
$feed .= '</channel>';
echo $feed;

The Content Type Header must be sent before any data can be translated by the server for the PHP-XML. Customize the Title of THIS File, removing the extensions, leaving just the Username. So, if the file was named mrjones.rss.php, it will now be just mrjones. Why? Simple, “one query to bind them all”, versus multiple queries. You can customize it many ways, for specific categories in your database like 
Home Improvement or Knick Knacks, so long as THIS MAIN FILE is named accordingly.

Now, with just the username, we mesh the PHP with the XML languages to create the display. Simply said, echoing the PHP. The beauty of it: when it is outputted, only the XML will be visible.
• Create the link to the Article.
• Get the location of the Photo to display.
• Build the RSS output ( .feed elements ).

Notice the main link is to the Username RSS, not the main website nor Template. Again, this is the Main Template. Save THIS file as rss.php. This will be used when the user registers, to automatically create the customized Username.rss.php for each one. It automatically copies the new file into the users folder. The last bit of code goes into the user generated pages, so visitors can click and subscribe.

$result= mysql_query
("SELECT * FROM `ARTICLES` WHERE TITLE='$TITLE'")
or die(mysql_error());
while($row=mysql_fetch_array($result))
{ $USERNAME = $row['USERNAME']; }
echo '<a href="http://...../rss/$USERNAME.rss.php">
Subscribe to $USERNAME 's Articles.</a>";

Huzzah!

BTW, this can also work in HTML5, since the structure is nearly the same. The issue is loading the file into a display page. My appraoch to this was a simple jQuery function to load the output elements into a container div.

$(function() {$('.ufeed').load('Username.rss.php'); });
Author: Charles James
Applied Philosopher and Programmer, specializing in PHP, jQuery, CSS. His approach to Web Development is a modern, minimalistic hybrid of logical-creative. Full-time web solutions developer, as well as part-time writer. "I enjoy teaching, learning and exploring new and challenging aspects of digital fandango!"