Welcome to Abdul Malik Ikhsan's Blog

Zend Framework : Zend_Feed_Writer – Generating Feeds

Posted in Tutorial PHP, Zend Framework by samsonasik on March 27, 2011

Zend_Feed_Writer adalah salah satu dari komponen Zend Framework yang bisa mengenerate RSS Feeds tanpa harus berpusing ria dengan XML. Arsitektur dari Zend_Feed_Writer sangatlah simple, ia terdiri dari 2 core class yaitu data container, dan renderer. Dalam container terdapat class Zend_Feed_Writer_Feed dan Zend_Feed_Writer_Entry. Kelas Entry dapat dilampirkan pada setiap kelas Feed. Tujuan utama container ini adalah untuk mengumpulkan data tentang feed untuk menghasilkan antarmuka yang sederhana menggunakan metode setter.
Ok, Anggaplah kita mempunyai data artikel di web kita yang akan kita tampilkan dalam RSS Feeds. Sebagai contoh seperti berikut :

$feed = new Zend_Feed_Writer_Feed;
$feed->setTitle('Testing Channel RSS');
$feed->setFeedLink('http://www.urldomainkita.com/rss', 'atom'); //url tempat kita buat rss ini ...
'name'  => 'domainkita',
'email' => 'contact@domainkita.com',
'uri'   => 'http://www.domainkita.com',
//waktu modify

//get data article ...( anggaplah kita sudah mempunyai method
//getDataArticle untuk mengambil 30 artikel terakhir
$artikel = new App_Model_Article();
$articledata = $artikel->getDataArticle(30);

//looping data artikel...
foreach($articledata as $key=>$row)
$entry = $feed->createEntry();
'name'  =>$row['author'],
'email' => $row['email'],
'uri'   =>  $row['authorsite'],

$datemodified = new Zend_Date($row['updated_at']);
$datecreated  = new Zend_Date($row['article_date']);

$entry->setDateModified( $datemodified->getTimestamp() );
$entry->setDateCreated( $datecreated->getTimestamp() );

$entry->setDescription("<img src=\"".$row['image']."\" align=left hspace=7 />".$row['content']);

//menambahkan entry ke feed...

$out = $feed->export('atom');

echo $out;

Semoga bermanfaat.

Image :

Referensi :

9 Responses

Subscribe to comments with RSS.

  1. Luciano said, on September 16, 2011 at 3:24 pm

    I like to know how was formatted the fields ‘updated_at’ and ‘article_date’ because I’m getting trouble to set the dates: setDateCreated and setDateModified.

    The mtf ZF is saying this: “Invalid Zend_Date object or UNIX Timestamp passed as parameter” but i’m passing exactly like you !

  2. samsonasik said, on September 16, 2011 at 6:05 pm

    I’m using format : Y-m-d H:i:s
    ex : 2010-11-25 20:07:15

  3. Luciano said, on September 17, 2011 at 1:20 am

    I Tried in some different forms and I simply cannnot make It show the correctly date.

    if($post->mdate == ‘0000-00-00 00:00:00’) $mdate = $post[‘cdate’];
    else $mdate = $post[‘mdate’];

    $datecreated = new Zend_Date($post[‘cdate’]);
    $datemodified = new Zend_Date($mdate);


    Nothing works😦

  4. samsonasik said, on September 17, 2011 at 9:08 am

    what ZF version you’re using ? I’m using 1.10.3. Hm…, Has you echo-ing your $mdate or $post[‘cdate’]; ? what the result ?

  5. Luciano said, on September 17, 2011 at 9:23 am

    I’m using 1.11.10
    When I echo-ing the vars the result is correct but when pass to zend feed it return the error and display a different date in this error😦

    I don’t know more what to do

  6. samsonasik said, on September 17, 2011 at 10:07 am

    hmm.., try to change to :

    if no error occured, the error is in parsing the timestamp(), try using basic PHP SPL🙂,
    use =>> strtotime

  7. samsonasik said, on September 17, 2011 at 10:09 am

    try $entry->setDateCreated(strtotime($post[‘cdate’]));

  8. nickmarinho said, on September 19, 2011 at 1:26 pm

    I tried so many choices that you sugested and that I imagine and no one works … but … today I look again the code and tried another one:

    $entry->setDateCreated(new Zend_Date(strtotime($post[‘cdate’])));

    And I don’t know if I havent tried it yet or another thing changes in server, but now its works fine.

    See http://www.lucianomarinho.com.br/rss.html

    Thank you very much for now.

  9. samsonasik said, on September 19, 2011 at 6:17 pm

    It was the least I could do, You’re welcome🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: