/** * @param ATOM|RSS2 $feed * @return ATOM|RSS2 */ protected function generateFeed($feed) { /* @var Post[] $posts */ $site_name = ArrayHelper::getValue(Yii::$app->params, 'site_name', Yii::$app->name); $posts = Post::find()->where(['status' => Post::STATUS_PUBLISHED])->orderBy(['post_time' => SORT_DESC, 'update_time' => SORT_DESC])->limit(20)->all(); $feed->setTitle($site_name); $feed->setLink(Url::home(true)); $feed->setSelfLink(Url::to(['feed/rss'], true)); $feed->setAtomLink(Url::to(['feed/atom'], true)); $feed->setDescription(ArrayHelper::getValue(Yii::$app->params, 'seo_description', '最新更新的文章')); if ($posts) { $feed->setDate($posts[0]->update_time); } else { $feed->setDate(time()); } foreach ($posts as $post) { $entry = $feed->createNewItem(); $entry->setTitle($post->title); $entry->setLink($post->getUrl(true)); $entry->setDate(intval($post->post_time)); $entry->setDescription($post->excerpt); $entry->setAuthor($post->author_name ? $post->author_name : $post->author->nickname); $entry->setId($post->alias); if ($feed instanceof ATOM) { $entry->setContent($post->content); } $feed->addItem($entry); } return $feed; }
* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ // IMPORTANT : No need to add id for feed or channel. It will be automatically created from link. //Creating an instance of ATOM class. $TestFeed = new ATOM(); //Setting the channel elements //Use wrapper functions for common elements $TestFeed->setTitle('Testing the RSS writer class'); $TestFeed->setLink('http://www.ajaxray.com/rss2/channel/about'); $TestFeed->setDate(new DateTime()); //For other channel elements, use setChannelElement() function $TestFeed->setChannelElement('author', array('name' => 'Anis uddin Ahmad')); //You can add additional link elements, e.g. to a PubSubHubbub server with custom relations. $TestFeed->setSelfLink('http://example.com/myfeed'); $TestFeed->setAtomLink('http://pubsubhubbub.appspot.com', 'hub'); //Adding a feed. Generally this portion will be in a loop and add all feeds. //Create an empty Item $newItem = $TestFeed->createNewItem(); //Add elements to the feed item //Use wrapper functions to add common feed elements
<?php /* @var $this \Level14\Website2Feed\SimpleView */ /* @var $feed Level14\Website2Feed\Model\Feed */ /* @var $items \Level14\Website2Feed\Model\Item[] */ use FeedWriter\ATOM; $atom = new ATOM(); $atom->setTitle($feed->getTitle()); $atom->setLink($feed->getUrl()); $atom->setDate($feed->getUpdatedDate()); $atom->setChannelElement('author', array('name' => $feed->getAuthor())); foreach ($items as $dbItem) { $item = $atom->createNewItem(); $item->setTitle($dbItem->getTitle()); $item->setLink($dbItem->getUrl()); $item->setDate($dbItem->getPublishedDate()); $item->setAuthor($feed->getAuthor()); $item->setDescription($dbItem->getDescription()); $atom->addItem($item); } $this->app->contentType('application/atom+xml'); $atom->printFeed();