PHPでGooglePlusのRSS取得
Googleがこの間始めたGooglePlus(Google+、https://plus.google.com/)というServiceがありますよね。
なんかTwitterやFacebookやMixiなどを足したServiceみたいなものです。
まあ未だに僕にはよくわからないのですけれどね…。
ところであれ、RSSFeedを表示しようとしても何処にもないんですよね。(僕が見つけられなかっただけかもしれないので見つけたら教えてください。)
ということでGoogleAPIを使ってPHPでRSS取得のProgramを書いてみました。
初めに、GoogleAPIを利用するには簡単な手続きをしなければなりません。
https://code.google.com/apis/console/にAccessして、CreateProjectをClick。
(左MenuのServiceというところをClickして、)API一覧の中から「Google+ API」というのをONにします。
なにやら同意文書が出てきますが、しっかり読んで同意します。
そして左MenuのAPI Accessというところを開くと「Simple API Access」という項目があります。
この赤いAPIKeyというのがあとで必要となります。
ではどうやってGooglePlusの投稿内容を取得するかというと、
https://www.googleapis.com/plus/v1/people/(UserID)/activities/public?alt=json&key=(APIKey)
このURLに取得したいUserのIDと先ほどのAPIKeyを指定するとJSON形式で情報が送られてきます。
UserIDとはなんかGooglePlusを表示したときのURLにある21桁くらいの数字です。
で、あとはこのJSONをPHPで分析してRSSに準じたXMLで吐き出せばよしというわけ。
まあJSONの要素はみているうちに何があるのか見えてくると思います。
ということで、今回は「SKE48 部屋っ子(https://plus.google.com/101064661667017020038/about)」を例にCodeを書いてみたので紹介します。
<?php
$src = json_decode(file_get_contents("https://www.googleapis.com/plus/v1/people/101064661667017020038/activities/public?alt=json&key=(Google APIKey)"), true);
header("Content-Type: text/xml; charset=utf-8");
print '<?xml version="1.0" encoding="utf-8" ?>'
?>
<rss version="2.0">
<channel>
<title>SKE48 部屋っ子</title>
<link>https://plus.google.com/101064661667017020038</link>
<description>SKE48 部屋っ子 GooglePlus RSS</description>
<language>ja</language>
<image>
<url>https://lh5.googleusercontent.com/-u5R9NXmE3Pk/AAAAAAAAAAI/AAAAAAAAAAA/jo77J5oKp0A/photo.jpg?sz=50</url>
<title>SKE48 部屋っ子</title>
<link>https://plus.google.com/101064661667017020038</link>
<width>50</width>
<height>50</height>
</image>
<lastBuildDate><?php print date("D, d M Y H:i:s +0900",
strtotime($src['items'][0]['published']) + (9 * 60 * 60)); ?></lastBuildDate>
<generator>http://software.hokt.net/</generator>
<?php
for ($i=0; $i<20; $i++) {
print "<item>\r\n";
$title = explode(" ", ereg_replace("\r|\n", " ", $src['items'][$i]['title']));
print "<title>" . trim($title[0]) . "</title>\r\n";
print "<description><![CDATA[ {$src['items'][$i]['title']} ]]></description>\r\n";
print "<link>{$src['items'][$i]['url']}</link>\r\n";
if (isset($src['items'][$i]['object']['attachments'][0]['fullImage'])) {
print "<enclosure url=\"{$src['items'][$i]['object']['attachments'][0]['fullImage']['url']}\" length=\""
. strlen(join('',file($src['items'][$i]['object']['attachments'][0]['fullImage']['url'])))
. "\" type=\"{$src['items'][$i]['object']['attachments'][0]['fullImage']['type']}\" />\r\n";
}
print "<comments>{$src['items'][$i]['object']['replies']['selfLink']}</comments>\r\n";
print "<pubDate>" . date("D, d M Y H:i:s +0900", strtotime($src['items'][$i]['published']) + (9 * 60 * 60))
. "</pubDate>\r\n"; // Ex : Wed, 04 Jan 2012 23:43:13 +0900
print "</item>\r\n\r\n";
}
?>
</channel>
</rss>
今回はRSSのHeader情報は手で書き、自動取得はしていません。なので汎用ではありません。
またこのほかにも色々取得できる情報がるので、試してみてください。


No comments yet.