CakePHP用のRestfulAPIコンポーネントを作ったので自分メモ

ゲーム用のマイミクランキング機能を作成。

全国ランキングだと得点すごいので、やる気なくすよね、きっと。
ってことでマイミク内の得点表示機能を追加しようかと安易な。。。

で、MYSQLと連携してCakePHPを使用しているので、使い回せるようにコンポーネント作成。
といってもほとんど公式サイトのPHPサンプルまんま。
変更点は下記。

0.Oauthを取得
http://code.google.com/p/oauth/
1.Consumer KeyとConsumer Secretを引数で渡した
2.Jsonで取得したマイミク情報をjson_decodeでPHPの変数に変換。
※マニュアル良く読まずに (array)json_decode($response)と、変換後object型からArrayにキャストして使用したが、
json_decode($response, true)と、2つめの引数にTrueで同じことだったっぽい。がーん。
3.一部mixi公式サイトに誤り。のため修正。
http://kishi-r.com/2010/01/26/mixi_restful_api/

(その後)
データ取得して、MYSQLのIDとフェッチするところがかなり面倒臭かった。。
array_multisort関数をはじめて使用。
普段SQLでorder by してるのでPHP内部で配列ソートする機会なんてなかった。。。
次回作成アプリから実装してみよー。今までのにも気が向いたら随時。。


restfulapi.php

<?php
require_once('***/lib/OAuth.php');

class RestfulapiComponent extends Object{
var $uses = array('***');
	public function get($VIEWER_ID,$PATTERN_CODE,$CONSUMER_KEY,$CONSUMER_SECRET)
	{
		$consumer = new OAuthConsumer($CONSUMER_KEY, $CONSUMER_SECRET, NULL);
		$user= $VIEWER_ID;
		/*
		 下記のような記述で情報を取得できる
		 /people/{guid}/@all
		 /people/{guid}/@friends
		 /people/{guid}/@self
		 /people/@me/@self
		 */
		if($PATTERN_CODE == '1'){
			$PATTERN_NAME = '@all';
		}else if ($PATTERN_CODE == '2'){
			$PATTERN_NAME = '@friends';
		}else if($PATTERN_CODE == '3'){
			$PATTERN_NAME = '@self';
		}
		$base_feed = 'http://api.mixi-platform.com/os/0.8/people/'.$VIEWER_ID.'/'.$PATTERN_NAME;
		$params = array(
			'xoauth_requestor_id' => $user,
			'filterBy'            => 'hasApp',
			'count'               => 100
		);
		$request = OAuthRequest::from_consumer_and_token($consumer, NULL, 'GET', $base_feed, $params);

		// Sign the constructed OAuth request using HMAC-SHA1
		$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);

		// Make signed OAuth request to the Contacts API server
		$url = $base_feed . '?' . $this->implode_assoc('=', '&', $params);
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_ENCODING, 'gzip');

		//$auth_header = $request->to_header();
		$auth_header = $request->to_header('api.mixi-platform.com');
		if ($auth_header) {
			curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header));
		}

		$response = curl_exec($curl);
		if (!$response) {
			$response = curl_error($curl);
		}
		curl_close($curl);

		return (array)json_decode($response);;
	}

	function implode_assoc($inner_glue, $outer_glue, $array, $skip_empty=false)
	{
		$output=array();
		foreach ($array as $key => $item) {
			if (!$skip_empty || $item) {
				$output[] = $key. $inner_glue. urlencode($item);
			}
		}
		return implode($outer_glue, $output);
	}
}

使い方

class TestController extends AppController{

var $components = array('Restfulapi');
function get_data(){
	$data = $this->Restfulapi->get($VIEWER_ID,1,$CONSUMER_KEY,$CONSUMER_SECRET);

	foreach($data['entry'] as $datas){
		$user_data = (array)$datas;
		$displayName = $user_data['displayName'];
		//色々な処理
	}
}