ブログパーツのwebサービスで良くあるRSS表示ツール。
そんなサービスを使えば、簡単に外部サイトのRSS情報を使い、外部リンクを画像付きで作ったり自動で更新できます。
しかし、広告が付いていたりデザインに制限があったりして、自分の思う通りにならない場合があります。
だったら自分で作ればいいじゃないかと思い、まずはRSSの情報を読み込み必要な情報だけ取り出す方法を探しました。
そして見つけたRSSの情報をパースしてくれる「MagpieRSS」の使い方を説明します。
参考サイト
PHP用のRSSパーサー MagpieRSS – 山本隆の開発日誌
RSSパーサーPHPライブラリ「MagpieRSS」の設定方法&サーバー設置方法 | web覚書
Magpie RSS – PHP RSS Parser によるRSSデータのパース処理
MagpieRSSの導入
Download magpierss-0.72.tar.gz (36.5 kB)をダウンロードしてください。
ダウンロード:https://sourceforge.net/projects/magpierss/files/
解凍したら、下記フォルダとファイルを残し削除してください。
extlib
rss_cache.inc
rss_fetch.inc
rss_parse.inc
rss_utils.inc
cacheフォルダを作成し任意のサーバーにアップロードします。
サーバーにアップロードするファイル構成は下記の通り。
■magpierss
├cache
├extlib
│└Snoopy.class.inc
├rss_cache.inc
├rss_fetch.inc
├rss_parse.inc
└rss_utils.inc
MagpieRSSの使い方
読み込みと設定
include_once('/magpierss/rss_fetch.inc');
define('MAGPIE_CACHE_ON', false); //キャッシュの使用有無
define('MAGPIE_CACHE_AGE', 0); //キャッシュ有効期限(秒数を設定)
define('MAGPIE_CACHE_DIR', '/magpie/cache/'); //キャッシュフォルダを指定
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); //文字コードの指定
MagpieRSSを読み込みキャッシュと文字コードの指定をします。
キャッシュを使用する場合、指定したフォルダにデータが保存されます。
RSSデータの取得
//RSS読み込み(読み込まれたデータはオブジェクト)
$root = fetch_rss('読み込むRSSのURL');
//itemsに格納された記事データを取得
$check = (array)$root->items;
if(empty($check)){
$itemObject = $root->channel->items;
}else{
$itemObject = $root->items;
}
//記事データの処理
foreach ($itemObject as $item) {
$title = htmlspecialchars_decode($item['title']);
$link = str_replace(array("\r\n","\n","\r"), '', htmlspecialchars_decode($item['link']));
//記事内容から画像を取得
$content = htmlspecialchars_decode($item['content']['encoded']);
$description = htmlspecialchars_decode($item['description']);
$summary = htmlspecialchars_decode($item['summary']);
preg_match('/(SRC|src)(=|\s=\s)[\"\']?(https?:\/\/[[:alnum:]\+\$\?\.%,!#~\*\/:@&=_\-亜-熙ぁ-んァ-ヶ]+(jpg|jpeg|png|gif))/i', $content, $contentMatches);
preg_match('/(SRC|src)(=|\s=\s)[\"\']?(https?:\/\/[[:alnum:]\+\$\?\.%,!#~\*\/:@&=_\-亜-熙ぁ-んァ-ヶ]+(jpg|jpeg|png|gif))/i', $description, $descriptionMatches);
preg_match('/(SRC|src)(=|\s=\s)[\"\']?(https?:\/\/[[:alnum:]\+\$\?\.%,!#~\*\/:@&=_\-亜-熙ぁ-んァ-ヶ]+(jpg|jpeg|png|gif))/i', $summary, $summaryMatches);
if(count($contentMatches)){
$imageLink = $contentMatches[3];
}elseif(count($descriptionMatches)){
$imageLink = $descriptionMatches[3];
}elseif(count($summaryMatches)){
$imageLink = $summaryMatches[3];
}else{
//記事内に画像が無い場合の処理
$imageLink = htmlspecialchars_decode($item['mainImage']);
}
//更新日時を取得
if(isset($item['pubdate']) && !empty($item['pubdate'])){
$date = strtotime(htmlspecialchars_decode($item['pubdate']));
if($date == false){
$dateArray = explode(" ", htmlspecialchars_decode($item['pubdate']));
array_pop($dateArray);
$dateArray[] = "+900";
$date = strtotime(implode(" ", $dateArray));
unset($dateArray);
}
}elseif(isset($item['date_timestamp']) && !empty($item['date_timestamp'])){
$date = $item['date_timestamp'];
}elseif(isset($item['dc']['date']) && !empty($item['dc']['date'])){
$date = strtotime(htmlspecialchars_decode($item['dc']['date']));
}elseif(isset($item['published']) && !empty($item['published'])){
$date = strtotime(htmlspecialchars_decode($item['published']));
}elseif(isset($item['issued']) && !empty($item['issued'])){
$date = strtotime(htmlspecialchars_decode($item['issued']));
}else{
$date = time();
}
//ここで取得したデータの処理をしてください。
}
取得したのは記事の「タイトル」「記事URL」「画像」「更新日時」です。
画像は記事内に最初に出てくる画像を取得します。
画像が無い場合はmainImageから取得しますが、無い場合もありますのでnoImageを用意するのが良いと思います。
更新日時はRSSの種類に対応しました。
まとめ
キャッシュ機能がありますが、沢山のRSSを毎回処理するのは負荷が大きくなるでしょう。
RSSデータを取得したらデータベースに保存して、そのRSSデータを使うのが再利用もしやすく負荷も小さくなると思います。
僕はcronを使い、定期的にRSS情報をチェックして取得するように作っています。