【CakePHP】iTunesのPodCast用のRSSを出力する
RSS2.0のメタデータ用拡張Dublin Coreを使いたくて、調べた結果をまとめます。
iTunesをタイトル名にいれたのは、そのほうがお客さんたくさんが来るかもしれない(www)からで、やり方は同じです
CakePHPの1.2.0.6311-betaの話です。
1.CakePHP1.2でRSSを出力する
すでに多くの方がまとめていらっしゃいますので、そちらを参照してください。
http://cakephp.jp/modules/newbb/viewtopic.php?viewmode=flat&topic_id=1067&forum=3
http://mt-systems.rdy.jp/mtsys/homes/view/cake12RSSFeed
2.RSS2.0を拡張する
そもそも「RSS2.0ってなあに」という、方はこちらを参照してください。
http://hail2u.net/documents/rss20notes.html
rss2.0はシンプルなのがいいところなのですが、物足りなくなった場合、モジュールを用いて拡張することもできます。

CakePHPでは、次のように代表的な拡張がすでに定義されています。
cake\libs\view\helpers\xml.php
var $__defaultNamespaceMap = array(
'dc' => 'http://purl.org/dc/elements/1.1/', // Dublin Core
'dct' => 'http://purl.org/dc/terms/', // Dublin Core Terms
'g' => 'http://base.google.com/ns/1.0', // Google Base
'rc' => 'http://purl.org/rss/1.0/modules/content/', // RSS 1.0 Content Module
'wf' => 'http://wellformedweb.org/CommentAPI/', // Well-Formed Web Comment API
'fb' => 'http://rssnamespace.org/feedburner/ext/1.0', // FeedBurner extensions
'lj' => 'http://www.livejournal.org/rss/lj/1.0/', // Live Journal
'itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd', // iTunes
'xhtml' => 'http://www.w3.org/1999/xhtml' // XHTML
);
もちろん、独自の拡張を定義して使うこともできます。
使い方
※すでに「1.CakePHP1.2でRSSを出力する」でRSSの配信ができているのが前提です。
「1.CakePHP1.2でRSSを出力する」で用意したapp\views\layouts\rss\default.ctpをコピーして、itunes.ctpを作成します。
次の行を追加します
( $rss->document($rss->addNs(名前空間名)); )
$rss->document($rss->addNs('itunes'));
独自の拡張やCakePHPに定義されていないそれ以外の拡張を使いたい場合は
$rss->document($rss->addNs(名前空間名,名前空間URL));
です
最終的にはapp\views\layouts\rss\itunes.ctpは次のとおりです。
<?php
echo $rss->header();
$rss->document($rss->addNs('itunes'));
if (!isset($channel)) {
$channel = array();
}
if (!isset($channel['title'])) {
$channel['title'] = $title_for_layout;
}
echo $rss->document(
$rss->channel(
array(), $channel, $content_for_layout
)
);?>
作成したレイアウトをアクションから使うように、app\controllers\コントローラ名.phpに次を加えます。
function アクション(){
$this -> layout = 'itunes';
}
これでrss要素が
<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" >
になりますので、あとは、出力データを整えるだけです。例えば、こんな感じで、app\views\コントローラ名\rss\アクション名.ctpを変更
echo $rss->items($posts, 'transformRSS'));function transformRSS($posts) {
return array(
‘title’ => $posts['Post']['title'],
‘link’ => array(’action’ => ‘view’, $posts['Post']['id']),
‘guid’ => array(’action’ => ‘view’, $posst['Post']['id']),
‘description’ => $posts['Post']['body'],
‘pubDate’ => $posts['Post']['created'],
‘itunes:author’ => $posts['Post']['itunes_author'],
‘itunes:duration’ => $posts['Post']['itunes_duration']
);
}
詳細なデータの形式はこちらを参照してください。
http://www.apple.com/itunes/store/podcaststechspecs.html#rss
Popularity: 18 %
by redgasuki 





(1 投票, 平均: 4 中 5)






コメントはまだありません。