RSS, in its current form, stands for Really Simple Syndication and is a family of web formats to publish frequently updated content. The RSS Feed (as it is commonly known) can then be read by the users feed reading software or by another website which wishes to syndicate the content of that feed.
The RSS format is based on XML that is built using standardised tags. Here is an example of a basic RSS document, we will be generating something similar using PHP later in this tutorial.
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>My RSS feed</title>
<link>http://www.mywebsite.com/</link>
<description>This is an example RSS feed</description>
<language>en-us</language>
<copyright>Copyright (C) 2009 mywebsite.com</copyright>
<item>
<title>My News Story 3</title>
<description>This is example news item</description>
<link>http://www.mywebsite.com/news3.html</link>
<pubDate>Mon, 24 Feb 2009 09:27:16 +0000</pubDate>
</item>
<item>
<title>My News Story 2</title>
<description>This is example news item</description>
<link>http://www.mywebsite.com/news2.html</link>
<pubDate>Wed, 27 Jan 2009 12:00:00 +0000</pubDate>
</item>
<item>
<title>My News Story 1</title>
<description>This is example news item</description>
<link>http://www.mywebsite.com/news1.html</link>
<pubDate>Wed, 30 Jan 2009 15:57:20 +0000</pubDate>
</item>
</channel>
</rss>
Take a look at the RSS Wikipedia page for a full history of RSS and it’s various different versions.
Creating the feeds:-
header(“Content-Type: application/rss+xml; charset=ISO-8859-1”);
header() function that we have called is telling PHP to output our data using the XML MIME type as well as to use the
ISO-8859-1 character set. This makes sure that our content is delivered to the users in the correct format.
Now we are going to define our database connection details and create the header XML tags for our RSS feed. I am going to statically assign the feed information tags just to keep it simple, however, you may wish to expand on this to dynamically set these. That way, you can re-use this code as a function or even go a step further and use it to build your own PHP class. The actual feed data will be kept in a variable called
$rssfeed.
DEFINE ('DB_USER', 'my_username');
DEFINE ('DB_PASSWORD', 'my_password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'my_database');
Next, we need to extract our data by looping through our MySQL database to create the
tags. I am not going to explain this part in too much details as it’s not point of this tutorial and you may do this differently. We are going to assume that our MySQL table (mytable) has columns called title, description, link and date which hold the relevant data:
<?php
include 'admin/db.php';
header("Content-Type: application/rss+xml; charset=ISO-8859-1");
$rssfeed = '<?xml version="1.0" encoding="UTF-8"?>';
$rssfeed .= '<rss version="2.0">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>beingIDEA | Programming Blog</title>';
$rssfeed .= '<link>http://www.beingidea.info</link>';
$rssfeed .= '<description>Programming Blog</description>';
$rssfeed .= '<language>en-us</language>';
$rssfeed .= '<copyright>Copyright (C) 2015 beingidea.info</copyright>';
$sql = "SELECT * FROM blogs ORDER BY id DESC LIMIT 20";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($query)) {
$name= strtr($row['name'], array('.' => '', ',' => '', '@' => '', '&' => ''));
$rssfeed .= '<item>';
$rssfeed .= '<title>' . $name . '</title>';
$rssfeed .= '<link>http://beingidea.info/blogs.php?slug='.$row['slug'].'</link>';
$rssfeed .= '</item>';
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
echo $rssfeed;
?>
I mentioned earlier that I would tidy up the .php extension of the feed. I don’t know about you, but I find this a little bit ugly. When you see RSS feeds that have been generated by WordPress for example, you don’t see the actual file name, you just get the containing folder. To do this, create a folder in the root directory of the site and call it feeds. Create a file in this new folder called index.php and copy the code above into it. This leaves us with a nicer looking feed URL, in the case of this example, http://www.mydomain.com/feed/.
It really isn’t that necessary to tidy the feed URL, but I think it just looks a little neater.
In this tutorial, I have write code to add custom meta query in main query…
This website uses cookies.