Subscribe

RSS Feed (xml)

Creating An RSS Feed With PHP And MySQL

Background

RSS or Really Simple Syndication truly is a simple XML markup used to standardize content distribution and syndication. Why use RSS? Syndicating your content through various feed directories and search engines widen your distribution. Also provide your content consumers with an easy way to keep up with updates and new content.
RSS has an easy to build Structure. Start with an xml tag that has the attributes version and encoding. This is followed by an opening rss tag with a version attribute. The main feed information is stored in the channel block. The basic feed information required is title, link, and description. Optionally an image tag can be added if you want to include a site logo etc. Item nodes contain the feed content. You create an item for each content piece, article, news release, etc. Items need nested title, link, and description nodes that are published.

Configure the Basic information

First setup the channel information. Base information required title, description, link, and URL.
$chan_title = "Higherpass.com";
$chan_desc = "Tutorials, articles, and howtos on a variety of computer related topics including php, perl, html, javascript, linux, and opensolaris.";
$chan_link = "http://www.higherpass.com/";
$image_url = "http://www.higherpass.com/images/hp_logo.gif";
 ?>

To keep the script simple and easy to update we store the title, description, and channel link at the beginning of the script. Also add the link to the image. The image section requires a title, url, and link sub-nodes. Both the title and link values need to match the channel values. This example will put the channel variables into the proper image XML tags.

Setup the Database

Example assumes a very simple naming scheme and should be adjusted accordingly.
$db_user = 'mysql';
$db_pass = 'mysql';
$db_host = 'localhost';
$db_database = 'content';
$db = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_database);
 ?>

Store mysql user, password, host, database name in the variables. Use mysql_connect and mysql_select_db connect to the database server and attach to the database.
Next setup and Perform Content Query
$query = "SELECT Content.Name, Content.Link, Content.Desc, Content.Posted FROM Content ORDER BY Content.Posted DESC LIMIT 25";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) < 1) {
    die("No records");
}
 ?>

Query the required information from the content table. If no rows were returned exit.

Build XML

The tutorial builds the XML out of order. This is done to easily insert the items into the larger structure. HEREDOC structures being used since snippets are simple and static in structure.
$items = '';
while ($row = mysql_fetch_assoc($result)) {
    $item_title = $row["Name"];
    $item_link = $row["Link"];
    $item_desc = $row["Desc"];
    $items .= <<
        
            $item_title
            $item_link
            $item_desc
        


ITEM;
}
 ?>

Initialize the items variable as an empty string, and loop through database records creating an item section for each. Each time through the loop the new item is appended to the $item variable. The minimum required fields are title, link, and description for an item.
Now put it all together.
$content = <<


        
                $chan_title
                $chan_link
                $chan_desc
                
                        $chan_title
                        $image_url
                        $chan_link
                
$items
        


CONTENT;
 ?>

Send to Client


header("Content-type: application/rss+xml");
print $content;
 ?>
The Content-type of application/rss+xml should be set by the header command prior to sending any non-header data to the client. Finally print the XML to the client.

Related Posts with Thumbnails