LaravelでRSSを配信したり、更新したりする話
RSSってのはちょっと古いですが、結構ニュース記事とか新着を確認するのが便利です。ま、RSSを知らない人にどんなの?と聞かれると、ニュースのタイトルがXMLになっているのですと答えればOKでしょう
https://www.soumu.go.jp/menu_kyotsuu/rss_information.html
さてさて、そんな感じで私はfeedlyを使っていますが、そんなのは別の話です。
今回は、このRSSを
・購読する。
・配信する。
と言うふたつです。
$ php -v
WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
PHP 7.3.24-(to be removed in future macOS) (cli) (built: Nov 23 2020 06:45:16) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.24, Copyright (c) 1998-2018 Zend Technologies
$ php artisan -v
Laravel Framework 8.19.0
購読する
購読は、別サービスが提供しているRSS配信を内部で処理させます。
仕事でRSS購読をしていますので調べなんてはいらないのですが、おさらいします。
利用するライブラリはこちらです
https://github.com/awjudd/l4-feed-reader
多分メジャーですねw
composerにてパッケージを入れます
composer.jsonに
"require": {
"php": "^7.3|^8.0",
"fideloper/proxy": "^4.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.12",
"laravel/tinker": "^2.5",
"awjudd/feed-reader": "*"
},
最後… なぜか1.1.0が入ります
Consoleでも良いかなと思いましたが、やり方がわからんのでWebページでチェック
行儀が悪いですが、読み込みチェック
Route::get('/', function () {
$feed = (new Awjudd\FeedReader\FeedReader)->read(
'http://www.soumu.go.jp/news.rdf'
);
foreach ($feed->get_items() as $item) {
\Illuminate\Support\Facades\Log::debug($item->get_date('Y/m/d') . ' : ' . $item->get_title());
}
return view('welcome');
});
ログに出力されればOK
[2020-12-17 08:35:24] local.DEBUG: 2020/12/16 : 武田総務大臣予算折衝・地方財政対策関係記者会見
[2020-12-17 08:35:24] local.DEBUG: 2020/12/16 : 情報通信審議会 情報通信技術分科会 放送システム委員会 地上デジタル放送方式高度化作業班(第7回)の開催について
[2020-12-17 08:35:24] local.DEBUG: 2020/12/16 : 第28回独立行政法人評価制度委員会 議事概要
[2020-12-17 08:35:24] local.DEBUG: 2020/12/15 : ブロードバンド基盤の在り方に関する研究会(第7回)開催案内
[2020-12-17 08:35:24] local.DEBUG: 2020/12/15 : 公共用周波数等WG(第1回)(非公開)
[2020-12-17 08:35:24] local.DEBUG: 2020/12/15 : 組織が発行するデータの信頼性を確保する制度に関する検討会(第7回)開催案内
[2020-12-17 08:35:24] local.DEBUG: 2020/12/15 : 国の資格の更新等に伴う講習・研修等の見直しに関する実態調査
書きながら思ったら、書いてたw
配信方法
次に、私のサイトが情報提供側にします。
配信のライブラリがあるのでそれを使います
https://gitlab.com/Laravelium/Feed
インストール方法は、各バージョンで異なります…
めんどくさかったら、コマンド一発
composer require laravelium/feed
すみません、今回バージョン確認せずに作っていますw
addItemに関しては悩みましたが、出来上がりです。
Route::get('feed', function() {
// create new feed
$feed = App::make("feed");
// cache the feed for 60 minutes (second parameter is optional)
$feed->setCache(60, 'laravelFeedKey');
if (!$feed->isCached()) {
$feed->title = 'Your title';
$feed->description = 'Your description';
$feed->link = url('feed');
$feed->setDateFormat('datetime'); // 'datetime', 'timestamp' or 'carbon'
$feed->lang = 'jp';
$feed->setShortening(true); // true or false
$feed->setTextLimit(100); // maximum length of description text
// set item's title, author, url, pubdate, description, content, enclosure (optional)*
$feed->addItem(['title'=>'算折衝・地方財政','description'=>'' , 'link' => 'https://gitlab.com/Laravelium/Feed' , 'author' => 'wataru','pubdate' => '2020/12/17 00;30']);
$feed->addItem(['title'=>'システム委員会', 'description'=>'' ,'link' => 'https://github.com/spatie/laravel-feed/issues/31' , 'author' => 'wataru','pubdate' => '2020/12/15 15:40']);
}
return $feed->render('rss');
});
これで、こんな感じが出ます
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:webfeeds="http://webfeeds.org/rss/1.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Your title</title>
<link>http://localhost:8000/feed</link>
<description><![CDATA[Your description]]></description>
<atom:link href="http://localhost:8000/feed" rel="self" type="application/rss+xml" />
<language>jp</language>
<lastBuildDate>Thu, 17 Dec 2020 11:03:25 +0000</lastBuildDate>
<item>
<title><![CDATA[算折衝・地方財政]]></title>
<link>https://github.com/spatie/laravel-feed/issues/31</link>
<guid isPermaLink="true">https://github.com/spatie/laravel-feed/issues/31</guid>
<description><![CDATA[]]></description>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">wataru</dc:creator>
<pubDate>2020/12/17 00;30</pubDate>
</item>
<item>
<title><![CDATA[システム委員会]]></title>
<link>https://github.com/spatie/laravel-feed/issues/31</link>
<guid isPermaLink="true">https://github.com/spatie/laravel-feed/issues/31</guid>
<description><![CDATA[]]></description>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">wataru</dc:creator>
<pubDate>2020/12/15 15:40</pubDate>
</item>
</channel>
</rss>
うん、できました!
これで、購読も、配信もできました!
超悩んだのは、やっぱり配信のaddItem….
公式情報では、addでOKらしいですw
// set item's title, author, url, pubdate, description, content, enclosure (optional)*
$feed->add($post->title, $post->author, URL::to($post->slug), $post->created, $post->description, $post->content);
https://gitlab.com/Laravelium/Feed/-/wikis/basic-feed
これではダメなのですよ。
ソースを解析すると、addItemに変わっていました。
項目に関してはTryAndErrorsでしたが、なんとか通りました。
んな感じで!次回!