Mobile app version of vmapp.org
Login or Join
Harper822

: Generating RSS Feeds Automatically? I am running a static PHP website for the past 3 months. It's not a CMS but just simple PHP files (no database) in to which I add content. I am struggling

@Harper822

Posted in: #Php #Rss

I am running a static PHP website for the past 3 months. It's not a CMS but just simple PHP files (no database) in to which I add content. I am struggling to manually create RSS feeds as it is very time consuming. Can someone suggest me ways to save time creating RSS feeds? I am also looking to automate the process of RSS feed creation, maybe using variables? Please suggest.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Harper822

2 Comments

Sorted by latest first Latest Oldest Best

 

@Bryan171

Yes it is possible to create an rss.xml feed automatically

This website is a static website and apparently it has an auto generated rss.xml
whatanswered.com/rss.xml
Validate the RSS feed
appc.w3.org/check.cgi?url=http%3A//whatanswered.com/rss.xml

Why don't you contact them via their website and ask them?

10% popularity Vote Up Vote Down


 

@Nimeshi995

As alluded to in the comments, you'll need a database holding your content to be able to automate your feed. We don't know from your question how comfortable with PHP you are or if you have any database experience, but this would almost certainly speed up other things in your site too - such as being able to serve each of your 'content' pages from one dynamic script, rather than trying to maintain several static pages.

I'm going to assume for a minute that you have MySQL and you know how to set up a table. Do that, and make sure your table (which i'll call "my_content" looks something like this:


item_id (int 6, auto increment)

item_title (char 255)

item_description (text)


In PHP, connect to your database - again, this is extremely simplified, you'll need to investigate further to add in some error checking and all that, but its just to point you in the right direction and show you how simple it can be to get started. (Also, MySQLi is the modern thing, but lets avoid OO for now..)

<?php
//Connect to a database server and find your database
mysql_connect($host,$user,$password);
mysql_select_db($db);

//Select the content for your feed
$SQL = "SELECT * FROM my_content ORDER BY item_id";
$QRY = mysql_query($SQL);

//Set up the XML header
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= ' <rss version="2.0">';
$xml .= " <channel>";

//Loop through the result set created in $QRY and add it into $xml
while ($ROW = mysql_fetch_assoc($QRY)) {
$xml .= "<item>";
$xml .= " <title>".$ROW['item_title']."</title>";
$xml .= " <description>".$ROW['item_description']."</description>";
$xml .= " <link>http://www.mysite.com/content.php?id=".$ROW['item_id']."</link>";
$xml .= "</item>";
}

//Add the XML footer
$xml .= " </channel>";
$xml .= " </rss>";

//Write the whole lot to a file.
file_put_contents("rss.xml",$xml);
?>


There you go. That's how easy it can be, although an RSS feed probably needs more information, I think thats about the bare minimum to create something that's valid. While you're welcome to copy/paste this bit of code - it won't win any programming awards, i've just tried to keep it extremely simple to give you the basic idea of how it works.

If you deploy anything on a real website, depending on many factors (not least who is using it, how they are putting content into the database, etc) you will want to work on things like error checking and data validation.

Hope it helps.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme