Recent News
Copyright © 2014 THAT Agency . All Rights Reserved.
410 Evernia Street, Suite 118, West Palm Beach, FL 33401
Tel: (561) 832-6262 | Fax: (561) 832-7707

Creating a dynamic RSS Feed using PHP and MySQL

24 Aug Creating a dynamic RSS Feed using PHP and MySQL

Ok, I know that there are plenty of classes, functions, pre-built scripts out there that will help you create an RSS feed to use with your website, but I have built my own and I will share it with all of you. This RSS page, will do a couple of things.

1. Grab the Title of your Article, Grab some Description, Grab the Link to your article and so on and so forth.
2. If by chance, any part of the article is in spanish, it will convert anything with an accent to regular letters so that the feed can actually validate.

You can check out the code here:

* Please note that in order for this to work, the server needs to know that it needs to look at this code that processes as XML…so place the RSS files into ONE folder…call it feeds or rss or something of that nature and edit the .htaccess file and place this next line of code in it:

AddType application/x-httpd-php .xml

————————-
All you have to do is change the parts needed, and rename it to whatever you want.
————————

<? header(‘Content-type: text/xml’); ?>

<? echo “<?xml version=”1.0″ encoding=”iso-8859-1″?”.”>”;
?>

<?

$today = date(“D, j M Y G:i:s T”);

/*print (‘<?xml version=”1.0″?>’);

*/

$rssdate = date(“r”);

print (”

<rss version=”2.0″ xmlns:dc=”http://purl.org/dc/elements/1.1/”>

<channel>

<title>TITLE OF THE FEED </title>

<link>LINK TO ACTUAL WEBSITE </link>

<description>DESCRIPTION OF THE FEED </description>

<language>en-us</language>

<pubDate>$today</pubDate>

<lastBuildDate>$today</lastBuildDate>

<docs>http://blogs.law.harvard.edu/tech/rss</docs>

<generator>YOU CAN PUT YOUR COMPANY NAME </generator>

<managingEditor>EMAIL@YOUREMAIL.COM</managingEditor>

<webMaster>EMAIL@YOUREMAIL.COM</webMaster>

“);

// prepare HTML text for use as UTF-8 character data in XML

function cleanText($intext) {

return utf8_encode(

htmlspecialchars(

stripslashes($intext)));

}

//Will Get snippet of Return Row depending how many words you want.

function ReturnSnippet ($string, $howmany)

{

$currString = explode(” “, $string);

for ($wordCounter=0; $wordCounter<$howmany; $wordCounter++) { echo $currString[$wordCounter].” “;
}

};

# Required to Connect to DB to get the information

# Opens DB Connection OR PLACE THE ACTUAL DB CONNECTION HERE.

require_once (“DBConn.php”);

$db = opendatabase();

# this function will gather all the information from the Authors table and
create a drop down of writers.

function BuildRSSFeed()

{

$sql = mysql_query(“SELECT id, categoryiD, title, body, datepublished
FROM tablename WHERE categoryID = ‘the id of the category you want to see’
ORDER BY id DESC”);

# THIS FOLLOWING FUNCTION WILL ACTUALLY CLEAN UP THE ACCENTS

function cleanUpAccents($str)

{

$patterns = array (“&aacute;”,”&eacute;”,”&iacute;”,”&oacute;”,”&uacute;”,”&Aacute;”,”&Eacute;”,”&Iacute;”,”&Oacute;”,”&Uacute;”);

$replace = array (“a”,”e”,”i”,”o”,”u”,”A”,”E”,”I”,”O”,”U”);

$stringReplaced = str_replace($patterns, $replace,$str);

return str_replace($patterns, $replace,$str);

}

while ($row = mysql_fetch_array($sql))

{

//Tue, 03 Jun 2003 09:39:21 GMT

$today = date(“D, j M Y G:i:s T”);

//$text = cleanText($row[body]);

print (”

<item>

<title>”);

$articleTitle = $row[title];

$title_accent = cleanUpAccents($articleTitle);

$title_final = $title_accent;

echo ($title_final);

print(“</title>

<link>http://www.yourdomain.com/whatever.php?id=$row[id]</link>

<description>”);

$text = ReturnSnippet ($row[body], “10”);

$text_accent = cleanUpAccents($text);

$finaltext = htmlentities(strip_tags($text_accent));

echo ($finaltext);

print (” </description>

<pubDate>$row[datepublished]</pubDate>

<author>EMAIL@EMAIL.com</author>

<guid>http://www.yourdomain.com/whatever.php?id=$row[id]</guid> #
this is a Globaly Unique IDentifier – meaning just put in the link of the article
thats NOT going to change.

</item>n”);

}

};

/*

<?=htmlentities(strip_tags() );
include(‘xml.php’);

$xml = XML_serialize();

*/

echo BuildRSSFeed();

print (”

</channel>

</rss>

“);

?>

Tags:
, ,
2 Comments
  • John
    Posted at 20:20h, 23 September

    Thank you for a great blog, I will be sure to bookmark it.

  • Bertram Caparoula
    Posted at 14:09h, 11 August

    Great rundown.. I like your style!