mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-03-03 16:24:28 +08:00
增加RSS订阅
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
namespace app\common\tools;
|
namespace app\common\tools;
|
||||||
|
|
||||||
use FeedWriter\RSS1;
|
use FeedWriter\RSS1;
|
||||||
|
use FeedWriter\RSS2;
|
||||||
|
|
||||||
class Rss
|
class Rss
|
||||||
{
|
{
|
||||||
@@ -18,7 +19,7 @@ class Rss
|
|||||||
$feed->setChannelAbout(Site::keywords());
|
$feed->setChannelAbout(Site::keywords());
|
||||||
|
|
||||||
// An image is optional.
|
// An image is optional.
|
||||||
$feed->setImage(Site::logo(), 'PHP武器库头像', get_source_link('/'));
|
$feed->setImage(Site::logo(), Site::name() . '头像', get_source_link('/'));
|
||||||
|
|
||||||
//Adding a feed. Generally this portion will be in a loop and add all feeds.
|
//Adding a feed. Generally this portion will be in a loop and add all feeds.
|
||||||
|
|
||||||
@@ -45,4 +46,82 @@ class Rss
|
|||||||
|
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function initRss2()
|
||||||
|
{
|
||||||
|
|
||||||
|
// Creating an instance of RSS2 class.
|
||||||
|
$feed = new RSS2();
|
||||||
|
|
||||||
|
// Setting some basic channel elements. These three elements are mandatory.
|
||||||
|
$feed->setTitle(Site::name());
|
||||||
|
|
||||||
|
$feed->setLink(Site::indexUrl());
|
||||||
|
|
||||||
|
$feed->setDescription(Site::desc());
|
||||||
|
|
||||||
|
|
||||||
|
// Image title and link must match with the 'title' and 'link' channel elements for RSS 2.0,
|
||||||
|
// which were set above.
|
||||||
|
$feed->setImage(Site::logo(), Site::name() . '头像', Site::indexUrl());
|
||||||
|
|
||||||
|
// Use the 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 is used for the
|
||||||
|
// optional "language" channel element.
|
||||||
|
$feed->setChannelElement('language', 'zh-cn');
|
||||||
|
|
||||||
|
$list_post = Site::mapRecentlyPost();
|
||||||
|
|
||||||
|
// The date when this feed was lastly updated. The publication date is also set.
|
||||||
|
$feed->setDate($list_post[0]->publish_time);
|
||||||
|
|
||||||
|
$feed->setChannelElement('pubDate', $list_post[0]->publish_time);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// If you want you can also add a line to publicly announce that you used
|
||||||
|
// this fine piece of software to generate the feed. ;-)
|
||||||
|
$feed->addGenerator();
|
||||||
|
|
||||||
|
// Here we are done setting up the feed. What's next is adding some feed items.
|
||||||
|
|
||||||
|
foreach ($list_post as $model_post) {
|
||||||
|
# code...
|
||||||
|
$new_item = $feed->createNewItem();
|
||||||
|
|
||||||
|
// Add basic elements to the feed item
|
||||||
|
// These are again mandatory for a valid feed.
|
||||||
|
$new_item->setTitle($model_post->title);
|
||||||
|
|
||||||
|
$new_item->setLink($model_post->read_url);
|
||||||
|
|
||||||
|
$new_item->setDescription($model_post->desc);
|
||||||
|
|
||||||
|
|
||||||
|
// The following method calls add some optional elements to the feed item.
|
||||||
|
|
||||||
|
// Let's set the publication date of this item. You could also use a UNIX timestamp or
|
||||||
|
// an instance of PHP's DateTime class.
|
||||||
|
$new_item->setDate($model_post->publish_time);
|
||||||
|
|
||||||
|
// If you want you can set the name (and email address) of the author of this feed item.
|
||||||
|
$new_item->setAuthor($model_post->author_name, $model_post->stie_contact_email);
|
||||||
|
|
||||||
|
// You can set a globally unique identifier. This can be a URL or any other string.
|
||||||
|
// If you set permaLink to true, the identifier must be an URL. The default of the
|
||||||
|
// permaLink parameter is false.
|
||||||
|
$new_item->setId($model_post->read_url, true);
|
||||||
|
|
||||||
|
// Now add the feed item to the main feed.
|
||||||
|
$feed->addItem($new_item);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// OK. Everything is done. Now generate the feed.
|
||||||
|
// Then do anything (e,g cache, save, attach, print) you want with the feed in $myFeed.
|
||||||
|
$content = $feed->generateFeed();
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -66,5 +66,31 @@ Route::rule('/rss1.xml', function (Request $request) {
|
|||||||
|
|
||||||
Cache::set($cache_key, $last_etag);
|
Cache::set($cache_key, $last_etag);
|
||||||
|
|
||||||
|
return xml($content)->eTag($last_etag);
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::rule('/rss2.xml', function (Request $request) {
|
||||||
|
|
||||||
|
$cache_key = 'rss2_cache_key';
|
||||||
|
|
||||||
|
$last_etag = Cache::get($cache_key);
|
||||||
|
|
||||||
|
if (!empty($cache_key)) {
|
||||||
|
|
||||||
|
$if_not_match = $request->header('If-None-Match');
|
||||||
|
|
||||||
|
if (!empty($if_not_match)) {
|
||||||
|
if ($if_not_match == $last_etag) {
|
||||||
|
return xml('', 304)->eTag($last_etag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = Rss::initRss2();
|
||||||
|
|
||||||
|
$last_etag = md5($content);
|
||||||
|
|
||||||
|
Cache::set($cache_key, $last_etag);
|
||||||
|
|
||||||
return xml($content)->eTag($last_etag);
|
return xml($content)->eTag($last_etag);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user