プログラム初心者のcakephp2.x・wordpress情報

プログラム初心者のcakephp2.xの技術情報・wordpressやAPI関連も覚書(PHP・mySQL)

cakePHP2.xでこの機能あるのかな?ちょっとざっと調べてみた。(RSS・PubSubHubbubって?)

RSSフィードの生成については標準機能で出来るようで、情報が多かった。
コレはすんなり出来そうだ。

 

まずRSSの拡張子を利用できるようにroutes.phpに1行追加する

Router::parseExtensions('rss');

RSSを配信したいコントローラーにRequestHandlerコンポーネントを追加
すでにコンポーネントを設定している場合は配列に「RequestHandler」を追加すればOK

public $components = array('RequestHandler');

RSSを配信したいコントローラー(仮にpost)に配信用のアクション(仮にfeed)を追加作成

app/Controller/PostsController.php
public function feed() {
    $this->Post->recursive = -1;
    $this->set('posts', $this->Post->find('all', array('limit'=>10, 'order'=>'Post.created desc')));
}

そしてviewの作成

app/View/Posts/rss/feed.ctp
$channel = array (
    'title' =--> 'サイトの名前',
    'link' => 'http://サイトURL/',
    'guid' => 'http://サイトURL/',
    'description' => 'フィードの説明',
    'rss_url' => 'フィードのURL'
); $this->set('channel',$channel); echo $this->Rss->items($posts, 'transformRSS'); function transformRSS($posts) { return array( 'title' => $posts['Post']['name'], //投稿のタイトル 'link' => array('action' => 'view', $posts['Post']['id']), //投稿ページへのリンク先 'guid' => array('action' => 'view', $posts['Post']['id']), //投稿ページへのリンク先 'description' => $posts['Post']['body'], //投稿の本文 'pubDate' => $posts['Post']['created'] //投稿日時 ); }

RSSの確認は「http://サイトURL/posts/feed.rss」にアクセスすると確認できます。
\app\View\Layoutsに設定したレアウトのheadに以下を追加すれば終了。

<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://サイトURL/posts/feed.rss" />

次が大変PubSubHubbubの実装・・・。結局決策の情報はなかった。

仕方なくRSSのdefault.ctpに必要な記述はべた書きで対応。もっとスマートな方法があったらぜひ修正したい。

 

app/View/Layouts/defaulr.ctp
if (!isset($channel)) {
    $channel = array();
}
if (!isset($channel['title'])) {
	$channel['title'] = $title_for_layout;
}

$this->Rss->document(
	$this->Rss->channel(
		array(), $channel, $this->fetch('content')
	)
);
echo '<?xml version="1.0" encoding="UTF-8">
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>'.$channel['title'].'</title>
<atom:link href="'.$channel['feedURL'].'" rel="self" type="application/rss+xml">
<link>'.$channel['link'].'</link>
<description>'.$channel['description'].'</description>
<language>ja</language>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com">
<atom:link rel="hub" href="http://superfeedr.com/hubbub">';
print_r($this->fetch('content'));
echo '</channel></rss>';

あとはレアウト.ctpに記述の追加(RSS 2.0の記述のしたあたりに追加)

<link rel="hub" href="http://pubsubhubbub.appspot.com/" />
<link rel="hub" href="http://superfeedr.com/hubbub" />