PHP generate RSS

23.Jul.2010

To create RSS feed on one of my clients website, I used PHP generate RSS script. I hope it will be useful for all of PHP developers who want to create RSS feed on custom developed website.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>PHP generate RSS feed</title>
        <link>http://example.com/</link>
        <description>PHP generate RSS feed</description>
        <language>de-de</language>
        <copyright>Copyright (C) <?php echo date('Y'); ?> example.com</copyright>
<?php
	$params = array('newest' => true, 'limit' => '0,10', 'where' => '1=1', 'order' => 'id DESC');
 
	//procedure for getting entities for your PHP generated RSS
	$listOfEntities = getList( $params );
 
	foreach($listOfEntities as $entity) :
?>
 
	<item>
            <title><?php echo $entity['title']; ?></title>
            <description><?php echo $entity['description']; ?></description>
            <link>http://example.com/<?php echo $entity['url']; ?></link>
        </item>
<?php
	endforeach;
?>
    </channel>
</rss>

Notice: If you would like to make it easy for engines to recognize your PHP generated RSS, you should include following snippet into section of your website:

<link rel="alternate" type="application/rss+xml" title="PHP generate RSS" href="http://example.com/rss/">