SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Valid RSS 2.0 Feed Generator

    I know some of you disagree with this method of RSS generation and prefer to use the default PHP stuff. 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 only tested 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 them here or email me at benkuhl [( at )] gmail dot com

    PHP Code:
    <?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(
                
    "&" => "&amp;",
                
    "–" => "&ndash;",
                
    "—" => "&mdash;",
                
    "¡" => "&iexcl;",
                
    "¿" => "&iquest;",
                
    "«" => "&raquo;",
                
    "<" => "&lt;",
                
    ">" => "&gt;",
                
    "$" => "$",
                
    "»" => "&laquo;",
                
    "¢" => "&cent;",
                
    "©" => "&copy;",
                
    "÷" => "&divide;",
                
    "µ" => "&micro;",
                
    "·" => "&middot;",
                
    "¶" => "&para;",
                
    "±" => "&plusmn;",
                
    "€" => "&euro;",
                
    "£" => "&pound;",
                
    "®" => "&reg;",
                
    "§" => "&sect;",
                
    "¥" => "&yen;",
                
    "’" => "'",
                
    "“" => "\"",
                
    "”" => "\""
            
    );
            
            foreach (
    $paramList as $key => $value) {
                if (!
    in_array($key$ignoreParam)) {
                    
    $paramList[$key] = str_replace(array_keys($replaceList), array_values($replaceList), $str);
                }
            }
            
            return 
    $paramList;
        }
    }
    ?>
    Usage:
    PHP Code:
    //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(); 
    I hope you find it useful!

  2. #2
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use this class, post a reply!

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will use this class for sure and will let you know my exp here.
    Thanks for posting.

  4. #4
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've added this to my development blog and will respond/support it from there. So if you have problems/suggestions please post them there.

    http://returntoquality.blogspot.com/...tor-class.html


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •