サンプル・プログラム
makeRSS.php | サンプル・プログラム本体。 |
解説:コンテンツの準備
1次元目 | 2次元目 | 内 容 |
---|---|---|
順序(数字) | uri | コンテンツURL |
title | タイトル | |
description | 記事 | |
latest | 更新日時(RFC3339形式) |
0080: //コンテンツ配列
0081: $Contents = array(
0082: 1 => array(
0083: "uri" => "https://www.pahoo.org/e-soul/webtech/php06/php06-21-01.shtm",
0084: "title" => "PHPで2つの文章の類似度を計算する(WebAPI版)",
0085: "description" => "PHPでYahoo! JAPAN WebAPIやKAKASIを利用し、異なる2つの文章の類似度を計算するプログラムを紹介する。2つの文章がまったく違うテーマを扱っているのかどうかを判定する際に役立つ。",
0086: "latest" => "2014-06-22T01:04:02+09:00"
0087: ),
0088: 2 => array(
配列の構造は上表の通り。

実際にRSSを作成するときには、コンテンツ・データベースから取得するなどすればよい。
解説:RSS生成
0197: /**
0198: * RSS 1.0を生成
0199: * @param array $contentsコンテンツ配列
0200: * @param string $temp RSSテンプレート
0201: * @return string XML文字列
0202: */
0203: function makeRSS10($contents, $temp) {
0204: global $NameSpace; //名前空間
0205: global $Sites; //サイト諸元
0206:
0207: //テンプレート読み込み
0208: $xml = simplexml_load_string($temp);
0209:
0210: //channel部作成
0211: $channel = $xml->addChild('channel', NULL, $NameSpace['RSS1.0']['xmlns']);
0212: $channel->addAttribute('rdf:about', $Sites['rss'], $NameSpace['RSS1.0']['rdf']);
0213: $channel->addChild('title', $Sites['title']);
0214: $channel->addChild('link', $Sites['domain']);
0215: $channel->addChild('description', $Sites['description']);
0216: $channel->addChild('language', 'ja', $NameSpace['RSS1.0']['dc']);
0217: $channel->addChild('creater', $Sites['creater'], $NameSpace['RSS1.0']['dc']);
0218: $channel->addChild('rights', $Sites['copyright'], $NameSpace['RSS1.0']['dc']);
0219: $channel->addChild('date', get_gmdate(time()), $NameSpace['RSS1.0']['dc']);
0220:
0221: $items = $channel->addChild('items');
0222: $items = $items->addChild('Sec', NULL, $NameSpace['RSS1.0']['rdf']);
0223:
0224: foreach ($contents as $content) {
0225: $e = $items->addChild('li', NULL, $NameSpace['RSS1.0']['rdf']);
0226: $e = $e->addAttribute('rdf:resource', $content['uri'], $NameSpace['RSS1.0']['rdf']);
0227: }
0228:
0229: //item部作成
0230: foreach ($contents as $content) {
0231: $item = $xml->addChild('item', NULL, $NameSpace['RSS1.0']['xmlns']);
0232: $item->addAttribute('rdf:about', $content['uri'], $NameSpace['RSS1.0']['rdf']);
0233: $item->addChild('title', $content['title']);
0234: $item->addChild('link', $content['uri']);
0235: $item->addChild('description', $content['description']);
0236: $item->addChild('date', $content['latest'], $NameSpace['RSS1.0']['dc']);
0237: }
0238:
0239: return $xml->asXML();
0240: }
最初に関数 simplexml_load_string を使い、RSS 1.0 のテンプレートをベースにXMLオブジェクトを生成する。

ここで注意するのが、XMLの名前空間(name space)を指定する変数 $NameSpace である。
「PHPでコロンを含むXML要素名を扱う方法」で紹介したが、コロンを含むXML要素は、コロンの前方が名前空間となっている。メソッド addChild や addAttribute を使って子ノードや属性を追加するときには、名前空間のURLを指定してやる必要がある。

生成したRSS 1.0は、asXMLメソッドを使って文字列として返す。
0242: /**
0243: * RSS 1.1を生成
0244: * @param array $contentsコンテンツ配列
0245: * @param string $temp RSSテンプレート
0246: * @return string XML文字列
0247: */
0248: function makeRSS11($contents, $temp) {
0249: global $NameSpace; //名前空間
0250: global $Sites; //サイト諸元
0251:
0252: //テンプレート読み込み
0253: $xml = simplexml_load_string($temp);
0254:
0255: //channel部作成
0256: $xml->addChild('title', $Sites['title']);
0257: $xml->addChild('link', $Sites['domain']);
0258: $xml->addChild('description', $Sites['description']);
0259:
0260: $items = $xml->addChild('items');
0261: $items->addAttribute('rdf:parseType', 'Collection', $NameSpace['RSS1.1']['rdf']);
0262:
0263: //item部作成
0264: foreach ($contents as $content) {
0265: $e = $items->addChild('item');
0266: $e->addAttribute('rdf:about', $content['uri'], $NameSpace['RSS1.1']['rdf']);
0267: $e->addChild('title', $content['title']);
0268: $e->addChild('link', $content['uri']);
0269: $e->addChild('description', $content['description']);
0270: }
0271:
0272: //xml宣言を除く
0273: return preg_replace("/\<\?xml version=\"1.0\"\?\>\n/u", '', $xml->asXML());
0274: }
参考サイト
- PHPで作るRSSビューア(ぱふぅ家のホームページ)
- PHPでコロンを含むXML要素名を扱う方法(ぱふぅ家のホームページ)
(2020年5月4日)PHP8対応,リファラチェック追加