From cc9627f932929ecba3a9a91c217b419e829464b2 Mon Sep 17 00:00:00 2001 From: augushong Date: Thu, 2 Mar 2023 09:55:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0RSS=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/tools/Rss.php | 81 +++++++++++++++++++++++++++++++++++++++- route/app.php | 26 +++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/app/common/tools/Rss.php b/app/common/tools/Rss.php index 88ab31c..c9fa08f 100644 --- a/app/common/tools/Rss.php +++ b/app/common/tools/Rss.php @@ -2,6 +2,7 @@ namespace app\common\tools; use FeedWriter\RSS1; +use FeedWriter\RSS2; class Rss { @@ -18,7 +19,7 @@ class Rss $feed->setChannelAbout(Site::keywords()); // 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. @@ -45,4 +46,82 @@ class Rss 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; + } } \ No newline at end of file diff --git a/route/app.php b/route/app.php index e689a0d..0e8a858 100644 --- a/route/app.php +++ b/route/app.php @@ -66,5 +66,31 @@ Route::rule('/rss1.xml', function (Request $request) { 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); }); \ No newline at end of file