Blog

"rss"

Items tagged with rss:

Creating Dynamic RSS Feeds in PHP

APR
12
2009
Posted in category Tutorials | 4513 views
Tags: tutorial  web  rss  xml  php 
RSS_icon.pngWell, here it is. My first tutorial. I've decided to write this tutorial on creating a basic RSS/XML feed in PHP because it's something I couldn't really find a good tutorial on to suit my needs, so hopefully this will help somebody out. The same technique can also be applied to create XML feeds for sitemaps, which is always good for search engines.

I'll assume you have content being fed from an SQL database, know how to connect to the database from your website to display the information, and this is the content you expect to be put into your feed. It's quite a basic tutorial, so aimed at PHP novices, as I guess many tutorials are as that's kind of the point.

To begin with, create a blank .xml file on your website, and give it full control access in your web host settings. Mine's in the root of my website - http://www.adamgleeson.co.uk/rss.xml.

Next, in your admin area, make the rss.php page. This needs to include your database connection information as usual.

Then from this point onwards, the RSS/XML content will be added to as one single variable in PHP. This means the file will effectively be one long line, but web browsers and feed readers will sort this out and make it into a sensible layout as long as all the tags are correct.

The XML file needs to start with a definition and a channel open tag:


$xml = "";
$xml .= "";


Note the ".=" to keep adding to what is previously in the variable without discarding it.

Continue by adding title and description tags for your feed. A title tag I would say is the minimum, but if you don't even want that you can skip this section I guess.

$xml .= "Adam Gleeson";
$xml .= "Animation and Computer Games";
$xml .= "http://adamgleeson.co.uk/";
$xml .= "© Adam Gleeson 2008";



Now for the feedy goodness. Grab your SQL from the database like you would for a normal page:


$sql = "SELECT * FROM table_name LIMIT 25";
$rs = mysql_query($sql);
while($rss = mysql_fetch_array($rs)){


And open up tags to start a new feed item:


$xml .= "";
$xml .=""; <br /> </div><br /> <br /> Add the title of the item, or whatever your table field heading is, then close the item tag.<br /> <br /> <div class="code"><br /> $xml .= $rss["title"];<br /> $xml .= "</title">;<br /> </div><br /> <br /> Next comes the description. In my description I just have the date and category, rather than reformatting the text:<br /> <br /> <div class="code"><br /> $xml .= "<description>";<br /> $xml .= $rss["date"];<br /> $xml .= "(";$xml .= $rss["category"];<br /> $xml .= ")";<br /> $xml .= "</description>";<br /> </div><br /> <br /> Last tag to be entered in my feed is the all-important link. Just to make it easier, I've linked it to the ID of the relevant item, which the page then uses PHP GET to decide what to display by grabbing the ID from the URL.<br /> <br /> <div class="code"><br /> $xml .= "<link>http://www.adamgleeson.co.uk/blog/?article=";<br /> $xml .= $rss["id"];<br /> $xml .= "</link>";<br /> </div><br /> <br /> Remember to close the item tag, then rinse and repeat as necessary. If all your data is contained within one table, this should be enough to display everything. The close bracket here is to end the while loop, while the close item tag only ends this current part of the feed, before continuing down the rest of the table as required.<br /> <br /> <div class="code"><br /> $xml .= "</item>";<br /> }<br /> </div><br /> <br /> <br /> Finally, close your channel and RSS tags that are still open.<br /> <br /> <div class="code"><br /> $xml .= "</channel>";<br /> $xml .= "</rss>"; <br /> </div><br /> <br /> Now to actually write the file. Using the empty file we created earlier, the PHP fwrite command is used to add the $xml variable we have just created into the file. <strong>Remember that PHP will only allow relative links in this stage</strong>. <br /> <br /> Last thing to do then is simply to close the PHP tag.<br /> <br /> <div class="code"><br /> $file = fopen("../../rss.xml", "w");<br /> fwrite($file, $xml);<br /> fclose($file);?><br /> </div><br /> <br /> To add some confirmation that it has been created (or rather, that nothing went wrong if you see no error), a simple link to the file is always helpful.<br /> <br /> <div class="code"><br /> RSS File Created.<br /> <p><br /> <a href="/rss.xml">View rss.xml</a><br /> </p><br /> </div><br /> <br /> And that's it, you should now have a working RSS feed. To make the feed visible in your web browser like on many websites, add this to your head tags on the pages you want the RSS feed to be visible:<br /> <br /> <div class="code"><br /> <head><br /> <link href="/rss.xml" rel="alternate" type="application/rss+xml" /><br /> </head><br /> </div><br /> <br /> <br /> As with most web things, there are many ways of doing the same thing, and this is simply one of them. I'm not suggesting it is the best way, but it works for me, so I hope this will help someone. I appreciate any comments, let me know if it's any use to you, or any requests for future tutorials! <div class="clear"></div> <h2>Related posts</h2> <div class="related"> <a href="/blog?article=377-_-Use-Javascript-to-interact-with-Flash"><span class="related_month"> June 17, 2010</span> <span class="related_link">Use Javascript to interact with Flash</span></a> <a href="/blog?article=375-_-Multiplayer-Blackjack-in-PHP,-Javascript,-MySQL"><span class="related_month"> June 13, 2010</span> <span class="related_link">Multiplayer Blackjack in PHP, Javascript, MySQL</span></a> <a href="/blog?article=374-_-My-favourite-Firefox-plugins"><span class="related_month"> June 03, 2010</span> <span class="related_link">My favourite Firefox plugins</span></a> <a href="/blog?article=352-_-CSS-Theme-Switcher"><span class="related_month"> May 29, 2010</span> <span class="related_link">CSS Theme Switcher</span></a> <a href="/blog?article=351-_-Template-websites-version-0.5"><span class="related_month"> May 03, 2010</span> <span class="related_link">Template websites version 0.5</span></a> </div><div class="clear"></div> <p> </p> <a class="right" href="/login?register=1&article=">    Register</a><a class="button right" href="/login?article=">Login</a> <h2> Comments [1] </h2> <div id="comments1"> <div class="comment_left "> #1 <span class="comment_name">Chris D</span> <br /><span class="comment_date">12/04/2009 - 18:30:57</span> <br /><a href="" class="comment_web"></a> </div> <div class="comment_right_container"> <div class="comment_right" > Thanks for taking the time to make this, Adam. I'll give it a go, should be helpful! </div> </div> <div style="clear:both;"> </div> <br /> <br /> <h2>Post a comment here</h2> <br /> <div class="contactbox" style="width:100%;"><a name="comment264" style="visibility:hidden;"></a> <form name="form1" method="post" action="#comment264"> <label><span>Name:</span><input name="name" class="textbox" type="text" value="" onfocus="clearDefault(this);"/></label> <label><span>Email:</span><input name="email" class="textbox" type="text" style=" " value="" onfocus="clearDefault(this);" /></label> <label><span>Website:</span><input name="website" class="textbox" type="text" value="[ optional ]" onfocus="clearDefault(this);"/></label> <label><span> five plus seven equals:</span> <input name="code" type="text" class="textbox" style=" " value="[ enter in numerical form ]" onfocus="clearDefault(this);" /></label> <label><span>Message:</span> <textarea name="comment" class="textarea" cols="45" rows="5" onfocus="clearDefault(this);"> </textarea><br /> </label> <label><span></span> <input type="submit" name="submit" value="Send" class="button" style="width:337px;" /> <input type="hidden" name="correct_code" value="12"/> <input type="hidden" name="count" value="1" /> <input type="hidden" name="parent_id" value="264" /> <input type="hidden" name="cmd" value="1" /></label> </form> </div> </div> </div> <div class="content_bottom"> </div> </div> <div class="clear"> </div> </div> <div class="blog_posts_bottom"></div> </div> <div class="clear"> </div> </div> <div class="body_bottom body_bottom_blog"></div> </div> </div> <div id="footer_holder"> <div id="footer"> <div id="footer_top"></div> <div id="footer_middle"> <div class="footer_content"> <div class="addthis_toolbox addthis_default_style"> <a href="http://www.addthis.com/bookmark.php?v=250&username=flux101" class="addthis_button_compact">Share</a> <span class="addthis_separator">|</span> <a class="addthis_button_twitter"></a> <a class="addthis_button_facebook"></a> <a class="addthis_button_google"></a> <a class="addthis_button_stumbleupon"></a> <a class="addthis_button_digg"></a> <a class="addthis_button_delicious"></a> <a class="addthis_button_blogger"></a> <a class="addthis_button_email"></a> </div> <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=flux101"></script> <a href="/rss.xml" class="rss"> </a> <a href="http://validator.w3.org/check?uri=referer" target="_blank" rel="nofollow"> <img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" border="0" style="margin-top:2px; float:left;" /> </a> <div class="right links"> <a class="button fancybox_iframe" href="/.includes/pages/contact.php?standalone=1">Contact Me</a> <a class="button" href="?mobile=on">iPhone version</a> </div> <div class="clear"> </div> <div class="footer_adam">© Adam Gleeson 2010</div> </div> </div> <div id="footer_bottom"></div> </div> </div></div> <div class="signedin"> <a href="/login/?r=blog"><strong>Sign in</strong></a> | <a href="/login/?register=1&r=blog">Register</a> </div> <div class="mobile_theme">Mobile theme on</div> <script type="text/javascript"> $(window).resize(function () { changeCSS($(window).width()); }); </script> </body> </html>