/** * @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; }
$TestFeed->setLink('https://github.com/mibe/FeedWriter'); $TestFeed->setDescription('This is just an example how to use the Feed Writer project in your code.'); // Image title and link must match with the 'title' and 'link' channel elements for RSS 2.0, // which were set above. $TestFeed->setImage('Testing & Checking the Feed Writer project', 'https://github.com/mibe/FeedWriter', 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Rss-feed.svg/256px-Rss-feed.svg.png'); // Use core setChannelElement() function for other optional channel elements. // See http://www.rssboard.org/rss-specification#optionalChannelElements // for other optional channel elements. Here the language code for American English and $TestFeed->setChannelElement('language', 'en-US'); // The date when this feed was lastly updated. The publication date is also set. $TestFeed->setDate(date(DATE_RSS, time())); $TestFeed->setChannelElement('pubDate', date(\DATE_RSS, strtotime('2013-04-06'))); // You can add additional link elements, e.g. to a PubSubHubbub server with custom relations. // It's recommended to provide a backlink to the feed URL. $TestFeed->setSelfLink('http://example.com/myfeed'); $TestFeed->setAtomLink('http://pubsubhubbub.appspot.com', 'hub'); // You can add more XML namespaces for more custom channel elements which are not defined // in the RSS 2 specification. Here the 'creativeCommons' element is used. There are much more // available. Have a look at this list: http://feedvalidator.org/docs/howto/declare_namespaces.html $TestFeed->addNamespace('creativeCommons', 'http://backend.userland.com/creativeCommonsRssModule'); $TestFeed->setChannelElement('creativeCommons:license', 'http://www.creativecommons.org/licenses/by/1.0'); // If you want you can also add a line to publicly announce that you used // this fine piece of software to generate the feed. ;-) $TestFeed->addGenerator(); // Here we are done setting up the feed. What's next is adding some feed items. // Create a new feed item. $newItem = $TestFeed->createNewItem(); // Add basic elements to the feed item // These are again mandatory for a valid feed. $newItem->setTitle('Hello World!'); $newItem->setLink('http://www.example.com');
/** * Get RSS feed * * Converts a JSON array of event objects into an RSS feed * * @param mixed $events Array of events to be converted * * @return string Complete markup for RSS feed */ public function RSSify($events) { $feed = new RSS2(); $this->getBuildings(null, false); $feed->setTitle('Upcoming Events'); $feed->setLink('http://events.uoit.ca'); $feed->setImage('Upcoming Events', 'http://events.uoit.ca', 'http://events.uoit.ca/img/uoit_logo.svg'); $feed->setDescription('Upcoming events, deadlines and more at the University of Ontario Institute of Technology'); $feed->setChannelElement('language', 'en-US'); $feed->setDate(date(DATE_RSS, time())); $feed->setSelfLink(currentURL); $feed->setAtomLink('http://feeds.feedburner.com/uoit/events', 'hub'); foreach ($events as $key => $event) { $time = date('g:i', strtotime($event['event_startdate'])); $ampm = date('a', strtotime($event['event_startdate'])) == 'am' ? 'a.m.' : 'p.m.'; $item = $feed->createNewItem(); $item->setTitle($event['event_name']); $item->setLink('http://events.uoit.ca/event/' . $event['id']); $item->setId($event['id'], false); $item->setDescription($event['event_description']); $item->setDate(date("Y-m-d H:i:s")); $item->addElement('date', strtotime($event['event_startdate'])); $item->addElement('time', $time . ' ' . $ampm); if ($event['contact_event_firstname'] && $event['contact_event_lastname'] && $event['contact_event_email']) { $item->setAuthor($event['contact_event_firstname'] . ' ' . $event['contact_event_lastname'], $event['contact_event_email']); } $item->addElement('location', $this->parseLocation($this->buildings, $event['location_campus'], $event['location_building'], $event['location_room'], $event['location_other'])); $feed->addItem($item); } $output = $feed->generateFeed(); return $output; }