Updating an RSS feed?

Associate
Joined
29 Mar 2004
Posts
593
Location
Cambs, UK
How do you go about updating an RSS feed?

What process do people use? For example, when a user adds content to their site, do they have a script that automatically adds that article to the rss feed?

Im currently developing a site in the evenings, and am going to be offering an rss feed, but i was wondering how to code it so that the rss feed is up to date with the articles on the site.

Also, how do you get it so that it deletes the last 'item' from the rss feed xml page, as your only aloud 15 items in a feed.

Any help or comments will be much appreciated.

Thanks,

Edward
 
Typically content will be stored in a database. Thus generating a 'Latest Articles' RSS feed is as simple as selecting the latest 15 articles from the DB. Naturally altering the select query will allow you to serve different feeds, such as 'Latest Politics Articles' and so on.

If your feed has few subscribers then you can do this every time the feed is requested. A script queries the database and formats the result set an an XML file as per the RSS spec.

If you're going to be experiencing high loads, then you would cache the XML file generated and serve that instead. You can set the XML file to update as required. You might add in some code to your new posting script that refreshes the XML file when you post a new article, or have it refreshed periodically (hourly cron job or such).

If you're not storing the content in a database, then generating the feed will have to be done by hand.
 
Yeh, my content is going in a mysql database. I dont think i'll have a vast amount of subscribers.

Any chance of some code, or know where i can look at example code to generate the rss code using php?

Thanks very much for the reply.

Edward
 
What would i name the rss feed (filename) if i wanted to link to it via a folder?

So i could link to it by:

Code:
Http://www.mydomain.com/rss

instead of having:

Code:
Http://www.mydomain.com/rss/rssfeed.xml

Thanks.
 
Conrad11 said:
What would i name the rss feed (filename) if i wanted to link to it via a folder?

So i could link to it by:

Code:
Http://www.mydomain.com/rss

instead of having:

Code:
Http://www.mydomain.com/rss/rssfeed.xml

Thanks.
Looks like a job for... mod_rewrite! :)

Of course Beansprout, oh how we love him, will probably come along and say this is like "using a mountain to cover a pothole." Irritating, but we love him really...
 
Last edited:
mod_rewrite aka using a mountain to cover a pothole :D

Make an rss folder and stick this in a .htaccess in it:
Code:
DirectoryIndex rssfeed.xml
:)
 
I would like to learn about mod-rewrite.

I have read through that articles a couple of times now, and I still don't fully understand how it could work (well i find it unbelievable), but I get the jist.

I am working on my site locally at the moment using XAMPP and I seem to have fallen at the first hurdle.

I am trying to make this url:

Code:
Http://localhost/shortcut

go to this url:

Code:
Http://localhost/cd/rss

I have loaded the Mod-ReWrite module as I can see from the phpinfo() in the xampp CP. I managed to do that following this, but there was a problem:

link above said:
We're going use this tiny module to achieve everything mentioned above. To use this module, first we have to enable it, since it's initially disabled in the configuration file. Open the httpd.conf file and uncomment the following lines (remove the trailing #s):

#LoadModule rewrite_module modules/mod_rewrite.so
#AddModule mod_rewrite.c

The first line tells Apache to load the mod_rewrite module, while the second one enables the use of it. After you restart Apache, mod_rewrite should be enabled, but not yet running.

But, I didn't see the second line in the httpd.conf file so I couldn't do the second bit.

I tried to continue anyways by adding this to the bottom of the file, but it just didn't work and i got an error saying there was nothing there.

Code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/shortcut$ /cd/rss
</IfModule>

What do I need to do?

Thanks...
 
Last edited:
Beansprout said:
mod_rewrite aka using a mountain to cover a pothole :D

Make an rss folder and stick this in a .htaccess in it:
Code:
DirectoryIndex rssfeed.xml
:)


This would hilarious if you hadn't have seen the hidden message

I hold out hope that you hadn't when you posted it
 
HAHAHAHAHAHAA I hadn't seen that :D :D :D

However, usage of obscure phrases means that nobody can ever truly predict what I'll say, at least so long as the edited by line is in action :p
 
Just been searching the old Google for the last hour and it seems many people have this problem, but i can't find a solution that works :/

Anywho, for the orginal guy, who may have thought that Beansprouts option was complicated. I have just Created a solution for me and you may want it.

It will only work if you store the stuff that goes in the RSS feed in databases:

Code:
<?php

	include 'db.php';

	$sql = mysql_query("SELECT * FROM `rss` ORDER BY `id` DESC LIMIT 0, 15");

	$file = fopen("feed.xml", 'w+') or die("can't open file");
	fwrite($file, "<?xml version=\"1.0\"?>
	<rss version=\"2.0\">
		<channel>
			<title>Title of Feed</title>
			<description>Decription of Feed</description>
			<link>Http://www.linktowebsite.com</link>");

	while($result = mysql_fetch_array($sql)){
		$title = $result['title'];
		$description = $result['description'];
		$link = $result['link'];
		fwrite($file, "
			<item>
				<title>$title</title>
				<description>$description</description>
				<link>$link</link>
			</item>");
	}

	fwrite($file, "
		</channel>
	</rss>");

	fclose($file);

	echo 'RSS Feed Updated';

?>

Just change all the database and SQL stuff (should be self explanitary) and have it run everytime you add a new article.

Hope it helps.


EDIT: My PHP isn't that good to be honest, so it could probably be re-written in a more "efficient" fashion.
 
Last edited:
Could you show me how to do that please, as that is what i tried originally and was unable to do it.

Maybe it's just the way i am carrying out the query but how can you get (function?) to add a string onto the end of a sting.

For instance in this bit:

Code:
	while($result = mysql_fetch_array($sql)){
	$title = $result['title'];
	$description = $result['description'];
	$link = $result['link'];
	$content = "
		<item>
			<title>$title</title>
			<description>$description</description>
			<link>$link</link>
		</item>");
	}

Everythime it loops, how can you get the content string to keep adding onto the end of the original $content string. Is there a function for this? I had a look at the Implode() function but i couldn't figure out how that would work.

Thanks.
 
Ah Cheers, ok this is what I have come up with:


Code:
<?php

	include 'db.php';

	$sql = mysql_query("SELECT * FROM `rss` ORDER BY `id` DESC LIMIT 0, 15");

	$xml = "<?xml version=\"1.0\"?>
	<rss version=\"2.0\">
		<channel>
			<title>Title of Feed</title>
			<description>Decription of Feed</description>
			<link>Http://www.linktowebsite.com</link>";	

	while($result = mysql_fetch_array($sql)){
	$title = $result['title'];
	$description = $result['description'];
	$link = $result['link'];
	$xml .= "
		<item>
			<title>$title</title>
			<description>$description</description>
			<link>$link</link>
		</item>";
	}

	$xml .= "
		</channel>
	</rss>";

	$file = fopen("index.xml", 'w+') or die("can't open file");
	fwrite($file, $xml);
	fclose($file);

	echo 'RSS Feed Updated';

?>
 
Last edited:
robmiller said:
This would hilarious if you hadn't have seen the hidden message

I hold out hope that you hadn't when you posted it
It would be... if his post wasn't edited after Beansprout posted :o
 
Back
Top Bottom