Saturday, April 2, 2011

How to create an rss feed with php

In order to create an rss feed with a php page on your site you will need to use the following template - and save it as rss.php or feed.php or something similar.

    header("Content-Type: application/rss+xml; charset=ISO-8859-1");

$dbh=mysql_connect ("localhost", "insertusernamehere", "insertpasswordhere") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("insertdbnamehere"); 

    $rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $rssfeed .= '<rss version="2.0">';
    $rssfeed .= '<channel>';
    $rssfeed .= '<title>Insert Title of Feed Here</title>';
    $rssfeed .= '<link></link>';
    $rssfeed .= '<description>Insert description here</description>';
    $rssfeed .= '<language>en-us</language>';
    $rssfeed .= '<copyright>Copyright (C) 2011 you</copyright>';

//query query insert query here for your data

not going to put my queries in but do yours here

      //get all the details
$quote_details = mysql_fetch_array($result);

$rssfeed .= '<item>';
        $rssfeed .= '<title>' . $quote_details["yourtitlevariable"] . '</title>';
        $rssfeed .= '<description>' . $quote_details["yourdescriptionvariable"] . '</description>';
        $rssfeed .= '<link></link>';
        $rssfeed .= '</item>';

    $rssfeed .= '</channel>';
    $rssfeed .= '</rss>';

    echo $rssfeed;

And then what you want to do is link your main page to your rss.php page like here:
<link rel="alternate" href="rss.php" title="title" type="application/rss+xml" />

Thats all! Enjoy!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
Web Design & Marketing - Click and get an instant quote on your project