Services_MixiAPIにフォトアルバム絡みのAPIを追加した
懲りずにやってますw
このシリーズの最初に出ていたフォトアルバムですが、アルバムアップロードAPIのURLをGETで叩いた、つまり、
http://photo.mixi.jp/atom/r=4/member_id=[mixiID]/album_id=[albumID]
にWSSE認証ヘッダ付きのGETリクエストを送ると、
<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title></title> <link rel="self" href="http://photo.mixi.jp/atom/r=4/member_id=xxxxxxx/album_id=xxxxxxxx" type="application/atom+xml"/> <link rel="alternate" href="http://mixi.jp/view_album.pl?id=xxxxxxxx" type="text/html"/> <author> <name></name> <url>http://mixi.jp/show_friend.pl?id=xxxxxxx</url> </author> <id>tag:mixi.jp,2007:photo/member_id=xxxxxxx/album_id=xxxxxxxx</id> <updated>2008-08-03T15:49:41Z</updated> <entry> <title>xxxxxxxx_3166100244.jpg</title> <link rel="alternate" href="http://mixi.jp/view_album_photo.pl?album_id=xxxxxxxx&number=3166100244&owner_id=xxxxxxx" type="text/html"/> <link rel="alternate" href="http://pc43.photo.mixi.jp/p/fb7bdea1d4447ce399c9622b441221981441d39baf/4895d300/album/98/33/xxxxxxxx_3166100244s.jpg" type="image/jpeg"/> <id>tag:mixi.jp,2007:photo/member_id=xxxxxxx/album_id=xxxxxxxx/photo_id=1</id> <updated>2008-08-03T15:49:41Z</updated> <summary/> <content src="http://pc46.photo.mixi.jp/p/dbd9b798a8e20c3100fae7b59cc15aa50a33257fdf/4895d300/album/98/33/xxxxxxxx_3166100244.jpg" type="image/jpeg"/> </entry> <entry> <title>xxxxxxxx_1604594023.jpg</title> <link rel="alternate" href="http://mixi.jp/view_album_photo.pl?album_id=xxxxxxxx&number=1604594023&owner_id=xxxxxxx" type="text/html"/> <link rel="alternate" href="http://pc48.photo.mixi.jp/p/c242563a46eef13f4d1778eec3241c596ffe62e5a3/4895d300/album/98/33/xxxxxxxx_1604594023s.jpg" type="image/jpeg"/> <id>tag:mixi.jp,2007:photo/member_id=xxxxxxx/album_id=xxxxxxxx/photo_id=2</id> <updated>2008-08-03T15:49:41Z</updated> <summary/> <content src="http://pc51.photo.mixi.jp/p/dfdf619d91f6d4fda9bdf6836331c2ea539e251795/4895d300/album/98/33/xxxxxxxx_1604594023.jpg" type="image/jpeg"/> </entry> : </feed>
なレスポンスを返してくることが分かりました。これはIDが[albumID]であるフォトアルバムに登録された写真一覧のようで、タイトルと画像へのURLが含まれています。
で、http://ido.nu/kuma/2007/06/29/mixi%E3%81%AE%E3%81%82%E3%81%97%E3%81%82%E3%81%A8api%E7%99%BA%E6%8E%98/のフォトアルバム絡みのAPIと、アルバムに属する写真一覧を取得するAPIをServices_MixiAPIに追加しました。
インストール・アップグレードは
$ sudo pear install -af http://www.doyouphp.jp/pear/Services_MixiAPI-0.2.1.tgz $ sudo pear upgrade -af http://www.doyouphp.jp/pear/Services_MixiAPI-0.2.1.tgz
な感じ。
使い方は次のとおりです。
<?php require_once 'Services/MixiAPI/Image.php'; require_once 'Services/MixiAPI/Factory.php'; error_reporting(E_ALL); /** * $user,$pass,$idを適宜書き換えてください */ $user = '[mixiのログインID]'; $pass = '[mixiのパスワード]'; $id = '[mixiのユーザーID]'; /** * フォトアルバム一覧APIオブジェクトの作成 */ $service = Services_MixiAPI_Factory::getInstance( Services_MixiAPI_Factory::API_MODE_ALBUMLIST, $user, $pass, $id); // リクエスト送信 $service->execute(); // 結果のXMLからアルバム名を取得 $xml = new SimpleXMLElement($service->get()); $xml->registerXPathNamespace('default', 'http://www.w3.org/2007/app'); $xml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom'); foreach ($xml->xpath('//default:collection[contains(@href, "/album_id=")]/atom:title') as $element) { echo $element . "\n"; } /** * フォトアルバムAPIオブジェクトの作成 */ $service = Services_MixiAPI_Factory::getInstance( Services_MixiAPI_Factory::API_MODE_PHOTOALBUM, $user, $pass, $id); // アルバムIDをセット $service->setAlbumId('xxxxxxxx'); // リクエスト送信 $service->execute(); // 結果のXMLから写真タイトルとURLを取得 $xml = new SimpleXMLElement($service->get()); $xml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom'); foreach ($xml->xpath('//atom:entry') as $element) { printf("%s(%s)\n", $element->title, $element->link['href']); } /** * アルバムに写真をアップロードするために、メソッドをセットする * デフォルトはServices_MixiAPI_PhotoAlbum::METHOD_LIST */ $service->setMethod(Services_MixiAPI_PhotoAlbum::METHOD_POST); // 写真オブジェクトをセット $service->setImage(new Services_MixiAPI_Image('/path/to/image.jpg')); // リクエスト送信 $service->execute(); // 結果のXML var_dump($service->get());
また、mixiツールバーのパケットをWIRESHARKで見ていたところ、マイミク一覧を取得するURLにQUERY_STRINGとして「last_login」が指定できるようで、「何分以内にログイン」を指定できるっぽいです。ただし、試したところ、60以外の値は指定できないようでした。
http://mixi.jp/atom/friends/r=1/member_id=[mixiID]?last-login=60
こちらもServices_MixiAPIに追加しました。使い方は次のとおり。
<?php require_once "Services/MixiAPI/Factory.php"; error_reporting(E_ALL); /** * $user,$pass,$idを適宜書き換えてください */ $user = '[mixiのログインID]'; $pass = '[mixiのパスワード]'; $id = '[mixiのユーザーID]'; /** * マイミク一覧 */ $service = Services_MixiAPI_Factory::getInstance( Services_MixiAPI_Factory::API_MODE_MYMIXI, $user, $pass, $id); // 60分以内にログインしたマイミクのみ $service->setLastLogin(60); $service->execute(); $xml = new SimpleXMLElement($service->get()); echo '<ul>'; foreach ($xml->entry as $entry) { printf( '<li><a href="%s" title="%sさん (%s)">%s</a>さん (%s)</li>', $entry->link[0]['href'], $entry->title, $entry->category['label'], $entry->title, $entry->category['label'] ); } echo '</ul>'; echo '<hr>';
コードは、いつも通りCodeReposに上げてあります。