efeed

Feed Writer Generator Extension (RSS 1.0, RSS 2.0, and ATOM 1.0)
36 followers

Feed Writer Generator Extension to create your feeds. Currently supports RSS 1.0, RSS 2.0 and ATOM 1.0.

I have created a GitHub repository for those willing to contribute on any of the extensions I created. Please, check the link at the bottom of this wiki.

Requirements

  • Developed using Yii version 1.1.5

Usage

Unzip the contents of the package and place it on your protected/extensions folder.

Examples of use

RSS 1.0

Yii::import('ext.feed.*');
 
// specify feed type
$feed = new EFeed(EFeed::RSS1);
$feed->title = 'Testing the RSS 1 EFeed class';
$feed->link = 'http://www.ramirezcobos.com';
$feed->description = 'This is test of creating a RSS 1.0 feed by Universal Feed Writer';
$feed->RSS1ChannelAbout = 'http://www.ramirezcobos.com/about';
// create our item  
$item = $feed->createNewItem();
$item->title = 'The first feed';
$item->link = 'http://www.yiiframework.com';
$item->date = time();
$item->description = 'Amaz-ii-ng <b>Yii Framework</b>';
$item->addTag('dc:subject', 'Subject Testing');
 
$feed->addItem($item);
 
$feed->generateFeed();

RSS 2.0

Yii::import('ext.feed.*');
// RSS 2.0 is the default type
$feed = new EFeed();
 
$feed->title= 'Testing RSS 2.0 EFeed class';
$feed->description = 'This is test of creating a RSS 2.0 Feed';
 
$feed->setImage('Testing RSS 2.0 EFeed class','http://www.ramirezcobos.com/rss',
'http://www.yiiframework.com/forum/uploads/profile/photo-7106.jpg');
 
$feed->addChannelTag('language', 'en-us');
$feed->addChannelTag('pubDate', date(DATE_RSS, time()));
$feed->addChannelTag('link', 'http://www.ramirezcobos.com/rss' );
 
// * self reference
$feed->addChannelTag('atom:link','http://www.ramirezcobos.com/rss/');
 
$item = $feed->createNewItem();
 
$item->title = "first Feed";
$item->link = "http://www.yahoo.com";
$item->date = time();
$item->description = 'This is test of adding CDATA Encoded description <b>EFeed Extension</b>';
// this is just a test!!
$item->setEncloser('http://www.tester.com', '1283629', 'audio/mpeg');
 
$item->addTag('author', 'thisisnot@myemail.com (Antonio Ramirez)');
$item->addTag('guid', 'http://www.ramirezcobos.com/',array('isPermaLink'=>'true'));
 
$feed->addItem($item);
 
$feed->generateFeed();
Yii::app()->end();

ATOM 1.0

Yii::import('ext.feed.*');
 
 
$feed = new EFeed(EFeed::ATOM);
 
// IMPORTANT : No need to add id for feed or channel. It will be automatically created from link.
$feed->title = 'Testing the ATOM RSS EFeed class';
$feed->link = 'http://www.ramirezcobos.com';
 
$feed->addChannelTag('updated', date(DATE_ATOM, time()));
$feed->addChannelTag('author', array('name'=>'Antonio Ramirez Cobos'));
 
$item = $feed->createNewItem();
 
$item->title = 'The first Feed';
$item->link  = 'http://www.ramirezcobos.com';
// we can also insert well formatted date strings
$item->date ='2010/24/12';
$item->description = 'Test of CDATA Encoded description <b>EFeed Extension</b>';
 
$feed->addItem($item);
 
$feed->generateFeed();

Change Log

  • v.1.3 Added stylesheet support
  • v.1.2 Added support for atom:link
  • v.1.1 Fixed bug on EFeedItemAtom thanks to Flokey82

Resources

Total 17 comments

#7178 report it
tonydspaniard at 2012/03/01 06:40am
@chuvbiz

I'll do it on my free time, thanks for pointing that out, but there are ways to tell people what to do or not, don't you think?

Moreover, there is a GitHub project where YOU can support the extension and help the community with the change.

#7177 report it
chuvbiz at 2012/03/01 06:23am
RSS1: tag CHANNEL must be open
$feed = new EFeed(EFeed::RSS1);
foreach ($news as $news_item) {
    $item = $feed->createNewItem();
    $item->title = $news_item->title;
    ...                      
    $feed->addItem($item);
}
$feed->generateFeed();

This code generate next:

<rdf:RDF xmlns:rdf="" xmlns="" xmlns:dc="">
<channel rdf:about=""/>
<title></title>
<link></link>
<description>
</description>
<ChannelAbout></ChannelAbout>
<items> ...

Why tag channel rdf:about=""/> is closed? "/>" Fix this bug!

#7053 report it
DarkNSF at 2012/02/21 01:38pm
Great extension, only one suggestion

Having a return rss content option would be nice. Like this:

This is useful for those who may want to cache

/**
     * 
     * Generates the Feed
     */
    public function generateFeed($return=false)
  {
    if (!$return)
    {
          header("Content-type: text/xml");
    }
 
        $returnString = $this->renderHead($return);
        $returnString .= $this->renderChannels($return);
        $returnString .= $this->renderItems($return);
        $returnString .= $this->renderBottom($return);
 
    if ($return)
    {
      return $returnString;
    }
    }
    /**
     * 
     * Prints the xml and rss namespace
     * 
     */
    private function renderHead($return=false)
    {
        $content  = '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL;
        if(!empty($this->stylesheets))
            $content .= implode (PHP_EOL, $this->stylesheets);
 
        if($this->type == self::RSS2)
        {
            $content .= CHtml::openTag('rss',array(
                        "version"=>"2.0",
                        "xmlns:content"=>"http://purl.org/rss/1.0/modules/content/",
                        "xmlns:wfw"=>"http://wellformedweb.org/CommentAPI/")).PHP_EOL;  
        }    
        elseif($this->type == self::RSS1)
        {
            $content .= CHtml::openTag('rdf:RDF',array(
                        "xmlns:rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
                        "xmlns"=>"http://purl.org/rss/1.0/",
                        "xmlns:dc"=>"http://purl.org/dc/elements/1.1/"
                    )).PHP_EOL;
        }
        else if($this->type == self::ATOM)
        {
            $content .= CHtml::openTag('feed', array("xmlns"=>"http://www.w3.org/2005/Atom")).PHP_EOL;
        }
 
    if ($return)
    {
      return $content;
    }
    else
    {
      echo $content;
    }
    }
    /**
     * 
     * Prints the xml closing tags
     * 
     */
    private function renderBottom($return=false)
    {
    $content = '';
 
        if($this->type == self::RSS2)
        {
            $content .= CHtml::closeTag('channel');
            $content .= CHtml::closeTag('rss');
        }    
        elseif($this->type == self::RSS1)
        {
            $content .= CHtml::closeTag('rdf:RDF');
        }
        else if($this->type == self::ATOM)
        {
            $content .= CHtml::closeTag('feed');                
        }
 
    if ($return)
    {
      return $content;
    }
    else
    {
      echo $content;
    }
    }
    /**
     * 
     * Prints the channels of the xml document
     * @throws CException
     */
    private function renderChannels($return=false)
  {
    $content = '';
 
        switch ($this->type) 
        {
           case self::RSS2: 
                $content .= '<channel>' . PHP_EOL;        
                break;
           case self::RSS1: 
                if(null !== $this->RSS1ChannelAbout )
                    $content .= CHtml::tag('channel',array('rdf:about'=>$this->RSS1ChannelAbout ));
                else 
                    $content .= CHtml::tag('channel',array('rdf:about'=>$this->link));
                break;
        }
 
        // Printing channel items
        foreach ($this->feedElements->itemAt('channels') as $key => $value) 
        {
            if($this->type == self::ATOM && $key == 'link') 
            {
                // ATOM prints link element as href attribute
                $content .= $this->makeNode($key,'',array('href'=>$value));
                // And add the id for ATOM
                $content .= $this->makeNode('id',$this->uuid($value,'urn:uuid:'));
            }
            else
            {
                $content .= $this->makeNode($key, $value);
            }    
 
        }
 
        // RSS 1.0 have special tag <rdf:Seq> with channel 
        if($this->type == self::RSS1)
        {
            if( null === $this->feedElements->itemAt('items') )
                throw new CException( Yii::t('EFeed', 'No items have been set') );
 
            $content .= "<items>" . PHP_EOL . "<rdf:Seq>" . PHP_EOL;
 
            foreach ($this->feedElements->itemAt('items') as $item) 
            {
                $tag = $item->link;
 
                if(null === $tag )
                    throw new CException( Yii::t('EFeed', 'For RSS 1.0 specifications link element should be add per item') );
 
                $content .= CHtml::tag('rdf:li',array('resource'=>$tag->content ),true).PHP_EOL;
            }
            $content .= "</rdf:Seq>" . PHP_EOL . "</items>" . PHP_EOL;
        }
 
    if ($return)
    {
      return $content;
    }
    else
    {
      echo $content;
    }
    }
    /**
     * 
     * Prints feed items
     * @throws CException
     */
    private function renderItems($return=false)
    {    
        if(null === $this->feedElements->itemAt('items'))
            throw new CException( Yii::t('EFeed', 'No feed items configured') );
 
    $content = '';
        foreach ($this->feedElements->itemAt('items') as $item)
            $content .= $item->getNode();
 
    if ($return)
    {
      return $content;
    }
    else
    {
      echo $content;
    }
    }
#6459 report it
tonydspaniard at 2012/01/12 04:41am
@Ben

Hi Ben,

About your requests, if you look at the change log you realize that it is already implemented as one of our colleagues did requested it before.

About your first request, custom namespaces, I will try to implement it asap, is a good idea. Nevertheless, if you wish to contribute to the extension, please fork it from the GitHub repository. Everybody welcome :)

#6451 report it
Ben at 2012/01/11 11:02am
feature requests

Hi, thanks for that extension, works very well. However, I'm missing two small features:

  1. Support to add custom namespaces (allows to extend RSS2 feeds with custom elements)
  2. Possibility to add Atom-Link self-reference in RSS2 feeds (suggested by w3c feed validator)

Any plans to extend this?

#5680 report it
tonydspaniard at 2011/11/01 01:57pm
@Sarke

Thank you very much for your input... will update for stylesheet support asap.

#5547 report it
Sarke at 2011/10/19 02:54pm
Styling support

Please add styling support. It goes in the header:

<?xml-stylesheet href="rss.xsl" type="text/xsl" media="screen"?>
#5426 report it
omarxp at 2011/10/11 01:32am
error has been fixed

@tonydspaniard error has been fixed, I upgraded to the latest framework yii

#5390 report it
tonydspaniard at 2011/10/09 06:02am
@omarxp

Please post your issues at the forum post. Will be happy to hear but it sounds you have an issue with your Yii configuration. CTypedMap is part of Yii core.

http://www.yiiframework.com/doc/api/1.1/CTypedMap

#5244 report it
omarxp at 2011/09/26 07:20am
Error..

i use this ext, and find error : include(CTypedMap.php): failed to open stream: No such file or directory,

can help me?

#4009 report it
tonydspaniard at 2011/05/27 02:21am
@cstdenis

Great suggestion!

I am in the middle of adding MRSS and iTunes support to the extension. Unfortunately I am very busy at the moment.

I will consider the dataProvider recommendation aswel. Very good feedback, thanks.

#3899 report it
cstdenis at 2011/05/18 08:50pm
Nice, class but could use some improvments.

I need MRSS (Media RSS) support so I tried to extend your class to add the necessary second xmlns, but I found $type and renderHead() private rather than protected preventing this.

You should change your private classes, when appropriate, protected to make it more extensible.

Even better, for my particular needs, would have been for renderHead() to either get the header from the appropriate EFeedItem class (not sure how you would structure that well) or to build it's data from a modifiable associative array.

I would also be nice to have it behave like classes such as CGridView where a dataProvider can be passed to it along with a list of column names and the items auto populated from that.

#3022 report it
danaluther at 2011/03/08 10:17am
Love It

This extension was very easy to get running, and quite effective. Thank you for sharing it.

#2895 report it
emilevdh at 2011/02/23 06:31am
ITunes

@tonydspaniard: Thanks! Looks like an awesome extension.

Please keep us posted with your progress with making your extension more universal.

We will need to use itunes specific tags.

#2631 report it
tonydspaniard at 2011/01/24 07:09am
@Sarke & @gusnips

Any feedback to create a better extension always welcome. I would love to make this RSS feed much more universal (iTunes for example?)

#2629 report it
Sarke at 2011/01/24 04:39am
Small and simple

Thank you! Very easy to use, and doesn't have to include parts of other frameworks to use! ;)

#2625 report it
gusnips at 2011/01/24 12:48am
Thanks for sharing

Looks great, I'll be definitively using it soon and then I'll give you my feedback

Leave a comment

Please to leave your comment.

Create extension