I've been searching Google and couldn't find anything except documentation. So I'm posting this here in hopes that it will help someone else from having to go through a few hours of Trial and Error getting their feed to validate. This is tested only to pass the
W3.org RSS 2.0 Validator.
I initially programmed it with required key values and things utilizing our custom api, but removed all of that for a stripped down basic version. If anyone has any updates/improvements to this feed or has any classes for other kinds of feeds I'd definitely be interested in hearing about them.
Post a comment if you use this and find it useful.
<?php
class FeedRss {
var $feed = "";
/*===========================================*/
/* Public Functions
/*===========================================*/
public function create ($paramList) {
$paramList = $this->cleanParamList($paramList);
$this->feed .= "<?xml version='1.0' encoding='iso-8859-1'?>\n";
$this->feed .= " <rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>\n";
$this->feed .= " <channel>\n";
$this->feed .= " <atom:link href='".$paramList['location']."' rel='self' type='application/rss+xml' />\n";
$this->feed .= " <title>".$paramList['title']."</title>\n";
if (array_key_exists("copyright", $paramList)) {
$this->feed .= " <copyright>".$paramList['copyright']."</copyright>\n";
}
$this->feed .= " <link>".$paramList['link']."</link>\n";
$this->feed .= " <description>".$paramList['description']."</description>\n";
if (is_numeric($paramList["updated"])) {
$this->feed .= " <lastbuilddate>".date('D, d M Y H:i:s O', $paramList['updated'])."</lastBuildDate>\n";
} else {
$this->feed .= " <lastbuilddate>".$paramList['updated']."</lastBuildDate>\n";
}
$this->feed .= " <language>".$paramList['language']."</language>\n";
if (array_key_exists("image", $paramList)) {
$imageInput = array_merge($paramList["image"], array(
"title" => $paramList["title"]
));
$this->addImage($imageInput);
}
}
public function addItem ($paramList) {
$paramList = $this->cleanParamList($paramList);
$this->feed .= " <item>\n";
$this->feed .= " <title><![CDATA[".$paramList['title']."]]></title>\n";
$this->feed .= " <link>".$paramList['link']."</link>\n";
$this->feed .= " <description><![CDATA[".$paramList['description']."]]></description>\n";
if (array_key_exists("author", $paramList)) {
$this->feed .= " <author>".$paramList['author']."</author>\n";
}
if (array_key_exists("guid", $paramList)) {
$this->feed .= " <guid>".$paramList['guid']."</guid>\n";
}
if (is_numeric($paramList["updated"])) {
$this->feed .= " <pubdate>".date('D, d M Y H:i:s O', $paramList['updated'])."</pubDate>\n";
} else {
$this->feed .= " <pubdate>".$paramList['updated']."</pubDate>\n";
}
if (array_key_exists("category", $paramList)) {
$this->feed .= " <category>".$paramList['category']."</category>\n";
}
$this->feed .= " </item>\n";
}
public function get () {
$this->feed .= " </channel>\n";
$this->feed .= " </rss>";
return $this->feed;
}
/*===========================================*/
/* Private Functions
/*===========================================*/
private function addImage ($paramList) {
$this->feed .= " <image>\n";
$this->feed .= " <url>".$paramList["url"]."</url>\n";
$this->feed .= " <width>".$paramList["width"]."</width>\n";
$this->feed .= " <height>".$paramList["height"]."</height>\n";
$this->feed .= " <title>".$paramList["title"]."</title>\n";
$this->feed .= " <link>".$paramList["link"]."</link>\n";
$this->feed .= " </image>\n";
}
private function cleanParamList ($paramList) {
$ignoreParam = array("updated", "description");
$replaceList = array(
"&" => "&",
"–" => "–",
"—" => "—",
"¡" => "¡",
"¿" => "¿",
"«" => "»",
"<" => "<",
">" => ">",
"$" => "$",
"»" => "«",
"¢" => "¢",
"©" => "©",
"÷" => "÷",
"ยต" => "µ",
"·" => "·",
"¶" => "¶",
"±" => "±",
"€" => "€",
"£" => "£",
"®" => "®",
"§" => "§",
"¥" => "¥",
"’" => "'",
"“" => "\"",
"”" => "\""
);
foreach ($paramList as $key => $value) {
if (!in_array($key, $ignoreParam)) {
$paramList[$key] = str_replace(array_keys($replaceList), array_values($replaceList), $str);
}
}
return $paramList;
}
}
?>
Usage:
//if writing to a file via cron or any other method, comment out this header
header("Content-Type: application/xml; charset=ISO-8859-1");
$feed->create(array(
"title" => "Domain.com News",
"copyright" => "Copyright (c) ".date("Y")." Site Inc. All rights reserved.",
"link" => "http://www.domain.com",
"location" => "http://feed.domain.com/feed.xml",
"description" => "All of the latest news",
"language" => "en-us",
"image" => array(
"url" => "http://www.domain.com/image/branding/rss-logo.jpg",
"height" => 50,
"width" => 50,
"title" => "RSS feed from Domain.com",
"link" => "http://www.domain.com"
)
));
//add as many items as you'd like
$feed->addItem(array(
"title" => $news["title"],
"link" => "http://www.domain.com/index.php?sec=news&act=view&id=".$news["id"],
"guid" => "http://www.domain.com/index.php?sec=news&act=view&id=".$news["id"],
"description" => $tempNewsItem["preview_cache"],
"author" => "news@domain.com (author)", //for proper validation you should have an email AND an author's name of some sort
"category" => "Computer Hardware",
"updated" => $news["posted"]
));
//this can also be used to write to a file
echo $feed->get();