2009/10/11
PHP:Mixiアプリモバイルの RESTful API を RESTful に使う
クラスを作ってみた。
Auth を気にすることなくサクサク書けます。
require_once('MixiAppMobileApi.php');
// APIのURLなどの詳細は以下を参照。
// http://developer.mixi.co.jp/appli/appli_mobile/lets_enjoy_making_mixiappmobile/for_partners
$personApi = 'http://****************************************';
$persistenceApi = 'http://****************************************';
$mixi = new MixiAppMobileApi;
// owner(viewer) データ取得
print_r($mixi->get($personApi));
// appData 追加
$mixi->post(
$persistenceApi,
array(
'score' => 530000,
'time' => time(),
)
);
// appData から score を削除
$mixi->delete($persistenceApi.'?fields=score');
// appData 取得
$myAppData = $mixi->get($persistenceApi);
print_r($myAppData);
3つのメソッドにURLをそえるだけ。なんて RESTful なんでしょう。
さておき、複数のアプリとか複数の環境で開発したりしていて、
Consumer を逐一書き換えるのが手間な場合は、以下のクラスメソッドを使えばOKです。
MixiAppMobileApi::initWithConsumer( '********************',//key '****************************************'//secret ); $mixi = new MixiAppMobileApi; print_r($mixi->get($personApi));
以下ソース MixiAppMobileApi.php
※ OAuth のライブラリが必要です。
<?
require_once('OAuth.php');
class MixiAppMobileApi{
static $consumer;
static $consumerKey = '';//設定して下さい
static $consumerSecret = '';//設定して下さい
static $appId = '';
static $ownerId = '';
var $rawData;
public static function initWithConsumer($key,$secret){
self::$consumerKey = $key;
self::$consumerSecret = $secret;
self::init();
}
static function init(){
if(isset($_GET['opensocial_app_id'])){
self::$appId = $_GET['opensocial_app_id'];
}
if(isset($_GET['opensocial_owner_id'])){
self::$ownerId = $_GET['opensocial_owner_id'];
}
self::$consumer = new OAuthConsumer(
self::$consumerKey,
self::$consumerSecret,
null
);
}
function MixiAppMobileApi(){
if(!isset(self::$consumer)){self::init();}
}
public function get($url) { return( $this->request($url) ); }
public function post($url,$data) { return( $this->request($url,$data) ); }
public function delete($url) { return( $this->request($url,null,'DELETE') ); }
function request($url,$data=null,$method=null){
switch(true){
case(isset($data)):
$options = array('method' => 'POST', 'content' => json_encode($data));
break;
case(empty($method)):
$options = array('method' => 'GET');
break;
default:
$options = array('method' => $method);
}
list($baseFeed,$queryString) = explode('?', $url, 2);
$parameters = $this->parameters($queryString);
$options['header'] = $this->header(
OAuthRequest::from_consumer_and_token(
self::$consumer,
null,
$options['method'],
$baseFeed,
$parameters
)
);
$this->rawData = file_get_contents(
$baseFeed . '?' . http_build_query($parameters,'','&'),
null,
stream_context_create(array('http' => $options))
);
return(json_decode($this->rawData));
}
function parameters($queryString=null){
$parameters = array(
'xoauth_requestor_id' => self::$ownerId,
);
if(isset($queryString)){
parse_str($queryString,$q);
$parameters += $q;
}
return($parameters);
}
function header($request){
$request->sign_request(
new OAuthSignatureMethod_HMAC_SHA1(),
self::$consumer,
null
);
$rows = array(
'Content-Type: application/json',
$request->to_header(),
'',
);
return(implode("\r\n",$rows));
}
function id($string){
list($null,$id) = explode(':',$string);
return($id);
}
}
コメント一覧