SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: RSS Generation

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

    RSS Generation

    I'm looking for a way to generate RSS feeds. I have my own script but it consistently has problems. Perhaps there is a class or api where I can call a few functions and input my data to output a valid RSS feed?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What problems are you experiencing?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Multiple. I can't get the author to show up, or the correct time format (which I think I have now actually). When I try to spread the feed to twitter it says I need a guid, which when I do put a GUID it breaks the rss feeds because it's not an actual link to our news.

    At this point, I'd prefer to utilize the work of someone else who's already figured all of that out.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    All those element are still going to be have to be supplied by you, regardless of library I'm afraid.

    Post the code you have so far, I'm sure all it will take is a tweak-or-two.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What about this, I haven't tested it, but it should put you on the right track. :-)
    PHP Code:
    class AtomFeed
    {
        protected $aEntries = array();
        
        protected $aMeta = array();
        
        public function __construct($sTitle, $sLink, $sAuthor)
        {
            $this->aMeta = array(
                'title'        => $sTitle,
                'link'        => $sLink,
                'author'    => $sAuthor
            );
        }
        
        public function addEntry(AtomEntry $oEntry)
        {
            array_push($this->aEntries, $oEntry);
            return $this;
        }
        
        public function render($bWithHeader = false)
        {
            if(true === $bWithHeader && false === headers_sent())
            {
                header('Content-type: application/atom+xml');
            }
            $sEntriesXML = '';
            foreach ($this->aEntries as $oEntry)
            {
                $sEntriesXML .= $oEntry->render();
            }
            return sprintf(
                trim(
                    '<?xml version="1.0" encoding="utf-8"?>
                    <feed xmlns="http://www.w3.org/2005/Atom">
                    <title>%s</title> 
                    <link href="%s"/>
                    <updated>%s</updated>
                    <author> 
                    <name>%s</name>
                    </author> 
                    <id>urn:uuid:%s</id>
                    %s
                    </feed>'
                ),
                htmlentities($this->aMeta['title']),
                $this->aMeta['link'],
                date('c'),
                htmlentities($this->aMeta['author']),
                md5($this->aMeta['title'] . $this->aMeta['author']),
                $sEntriesXML
            );
        }
    }
    class AtomEntry
    {
        protected $aMeta = array();
        
        public function __construct($sTitle, $sLink, $sSummary)
        {
            $this->aMeta = array(
                'title'        => $sTitle,
                'link'        => $sLink,
                'summary'    => $sSummary
            );
        }
        
        public function render()
        {
            return sprintf(
                trim(
                    '<entry>
                        <title>%s</title>
                        <link href="%s"/>
                        <id>urn:uuid:%s</id>
                        <updated>%s</updated>
                        <summary>%s</summary>
                    </entry>'
                ),
                htmlentities($this->aMeta['title']),
                $this->aMeta['link'],
                md5($this->aMeta['title'] . $this->aMeta['summary']),
                date('c'),
                htmlentities($this->aMeta['summary'])
            );
        }
    }
    Usage
    PHP Code:
    $oFeed = new AtomFeed('My Feed''http://www.sitepoint.com/''SilverBulletUK');
    $oFeed->addEntry(new AtomEntry('Article Name 0''http://www.sitepoint.com/article''My Summary 0'));
    $oFeed->addEntry(new AtomEntry('Article Name 1''http://www.sitepoint.com/article''My Summary 1'));
    $oFeed->addEntry(new AtomEntry('Article Name 2''http://www.sitepoint.com/article''My Summary 2'));
    $oFeed->addEntry(new AtomEntry('Article Name 3''http://www.sitepoint.com/article''My Summary 3'));
    $oFeed->addEntry(new AtomEntry('Article Name 4''http://www.sitepoint.com/article''My Summary 4'));
    echo 
    $oFeed->render(true); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the example, but even that is missing things. I see some RSS feeds that have CDATA escaping and pubDate and fields like that. Here's what I've got:

    PHP Code:
    <?php
    /**
     * @class Tru.Feed.Rss
     * Creates 
     * @constructor
     * @param {Tru} $tru The base Tru object
     */
    class TruFeedRss extends TruFeed {
        var 
    $feed "";
        
    /*===========================================*/
        /* Built In Functions
        /*===========================================*/
        
    function __construct (&$tru) {
            
    $this->tru = &$tru;
        }
        
    /*===========================================*/
        /* Public Functions
        /*===========================================*/
        
    public function create ($paramList) {
            
    $parameterList = array(
                
    "title",
                
    "link",
                
    "description"
            
    );
            if (!
    $this->tru->utility->checkKey($paramList$parameterList)) {
                
    $this->tru->error->requireParameter(array(
                    
    "call" => "Tru.Feed.Rss::create()",
                    
    "supplied" => $paramList,
                    
    "required" => $parameterList,
                    
    "terminate" => true
                
    ));
                
                return 
    false;
            }
            
            
    $paramList $this->tru->apply(array(
                
    "updated" => time(),
                
    "language" => "en-us"
            
    ), $paramList);
            
            
    $paramList $this->cleanParamList($paramList);
            
            
    $this->feed .= "<?xml version='1.0' encoding='utf-8'?>\n";
            
    $this->feed .= "    <rss version='2.0'>\n";
            
    $this->feed .= "        <channel>\n";
            
    $this->feed .= "        <title>".$paramList['title']."</title>\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";
        }
        
        public function 
    addItem ($paramList) {
            
    $parameterList = array(
                
    "title",
                
    "link",
                
    "description"
            
    );
            if (!
    $this->tru->utility->checkKey($paramList$parameterList)) {
                
    $this->tru->error->requireParameter(array(
                    
    "call" => "Tru.Feed.Rss::addItem()",
                    
    "supplied" => $paramList,
                    
    "required" => $parameterList,
                    
    "terminate" => true
                
    ));
                
                return 
    false;
            }
            
            
    $paramList $this->cleanParamList($paramList);        
            
            
    $this->feed .= "        <item>\n";
            
    $this->feed .= "            <title>".$paramList['title']."</title>\n";
            
    $this->feed .= "            <link>".$paramList['link']."</link>\n";
            
    $this->feed .= "            <description>".$paramList['description']."</description>\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";
            }
            
            
    $this->feed .= "        </item>\n";
        }
        
        public function 
    get () {
            
    $this->feed .= "        </channel>\n";
            
    $this->feed .= "    </rss>";
            
            return 
    $this->feed;
        }
        
        private function 
    cleanParamList ($paramList) {
            
    $paramListModified $this->tru->utility->removeKey($paramList"updated");
            
    $paramListModified $this->tru->utility->text->clean($paramListModified, array(
                
    "html" => true,
                
    "quote" => false,
                
    "slashes" => false,
                
    "script" => false,
                
    "truncate" => false
            
    ));
            
    $paramListModified["updated"] = $paramList["updated"];
            
            return 
    $paramListModified;
        }
    }
    ?>
    $this->tru->utility->text->clean refers to a function which cleans unwanted elements as you can see in this case I don't want it to clean anything except html.

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What specifically are you trying to do? Are you trying to convert html to RSS or a database to RSS. If its the latter there are scripts like SQL2RSS that will dynamically update the feed as the database updates.

  8. #8
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can find many classes out there who creates rss feed, check joomla for example


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
  •