关键词: Dena モバゲー OpenSoccial OAuth RESTful curl
OAuth.php
本家サイトからダウンロードしてください。(http://code.google.com/p/oauth/)
Denaのモバゲーの仕様要求により、【xoauth_requestor_id】は必要があるので、少し改修があり
[sourcecode language=’php’]
/**
* builds the Authorization: header
*/
public function to_header($realm=null,$xoauth_requestor_id) {
$first = true;
if($realm) {
$out = ‘Authorization: OAuth realm=”‘ . OAuthUtil::urlencode_rfc3986($realm) . ‘”‘;
$first = false;
} else
$out = ‘Authorization: OAuth’;
$total = array();
foreach ($this->parameters as $k => $v) {
if (substr($k, 0, 5) != “oauth”) continue;
if (is_array($v)) {
throw new OAuthException(‘Arrays not supported in headers’);
}
$out .= ($first) ? ‘ ‘ : ‘,’;
$out .= OAuthUtil::urlencode_rfc3986($k) .
‘=”‘ .
OAuthUtil::urlencode_rfc3986($v) .
‘”‘;
$first = false;
}
$out .= ‘,’. OAuthUtil::urlencode_rfc3986(‘xoauth_requestor_id’) .
‘=”‘ .
OAuthUtil::urlencode_rfc3986($xoauth_requestor_id) .
‘”‘;
return $out;
}
public function __toString() {
return $this->to_url();
}
public function sign_request($signature_method, $consumer, $token , $xoauth_requestor_id) {
$this->set_parameter(
“oauth_signature_method”,
$signature_method->get_name(),
false
);
$this->set_parameter(
“xoauth_requestor_id”,
$xoauth_requestor_id,
false
);
$signature = $this->build_signature($signature_method, $consumer, $token);
$this->set_parameter(“oauth_signature”, $signature, false);
}
[/sourcecode]
APICall.php
[sourcecode language=’php’]
class APICall {
private $consumer_key = ‘Your consumer_key ‘;
private $consumer_secret = ‘Your consumer_secret’;
private $reqOrg;
private $xoauth_requestor_id ;
private $debugFlg;
private $mode;
function __construct($debugFlg=false,$mode = null) {
$this->reqOrg = OAuthRequest::from_request(NULL, NULL, NULL);
$this->debugFlg = $debugFlg;
$this->mode = $mode;
if($this->mode == ‘Trusted’) {
$this->xoauth_requestor_id = ‘Your App ID’;
}else {
$this->xoauth_requestor_id = $this->reqOrg->get_parameter(‘opensocial_viewer_id’);
}
}
public function makeHeaders($url,$method) {
$consumer = new OAuthConsumer($this->consumer_key, $this->consumer_secret, NULL);
if($this->mode == ‘Trusted’) {
$token = null;
}else {
$token = new OAuthToken(
$this->reqOrg->get_parameter(‘oauth_token’),
$this->reqOrg->get_parameter(‘oauth_token_secret’));
}
parse_str(parse_url($url, PHP_URL_QUERY), $params);
$request = OAuthRequest::from_consumer_and_token($consumer, $token, $method, $url, $params);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),$consumer, $token,$this->xoauth_requestor_id);
$auth_header = $request->to_header(“”,$this->xoauth_requestor_id);
return $auth_header;
}
public function fetchAPI($url,$method,$headers,$fields) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if($this->debugFlg) {
curl_setopt($ch, CURLOPT_HEADER, true);
}else {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
switch($method) {
case ‘GET’:
curl_setopt($ch, CURLOPT_HTTPHEADER, array($headers));
break;
case ‘POST’:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Content-Type: application/json; charset=utf-8”,$headers));
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($fields));
break;
case ‘PUT’:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Content-Type: application/json; charset=utf-8”,$headers));
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($fields));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
break;
case ‘DELETE’:
curl_setopt($ch, CURLOPT_HTTPHEADER, array($headers));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
break;
}
$ch_data=curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($status < 400) {
return $ch_data;
}else {
return null;
}
}
public function callAPI($url,$method,$fields=null) {
$headers = $this->makeHeaders($url,$method);
$res = $this->fetchAPI($url,$method,$headers,$fields);
return $res;
}
}
?>
[/sourcecode]
CommonAPI.php(Sample)
[sourcecode language=’php’]
function getFriendsList() {
if(RUN_MODE) {
$url=self::API_URL.”api/restful/v1/people/$this->viewid/@friends?fields=id,nickname,thumbnailUrl&amp;filterBy=hasApp&amp;filterOp=equals&amp;filterValue=true&amp;count=40&amp;format=json”;
$method = ‘GET’;
$client = new APICall();
$res = $client->callAPI($url,$method);
if(!empty($res)) {
$json_data = json_decode($res, true);
$this->friendsInfo = $json_data[‘entry’];
for($i=0;$i
$old_id = $this->friendsInfo[$i][‘id’];
$this->friendsInfo[$i][‘id’] = substr($old_id,strrpos($old_id,’:’)+1);
}
}else {
$this->friendsInfo = null;
}
}else {
$this->createTestFriends();
}
return $this->friendsInfo;
}
function addTextDataGroup($group_name) {
$url=self::API_URL.”api/restful/v1/textdata/@app/@all”;
$method = ‘POST’;
$fields = array(
‘name’=> $group_name
);
$client = new APICall(false,’Trusted’);
$client->callAPI($url,$method,$fields);
}
function getTextDataGroup() {
$url=self::API_URL.”api/restful/v1/textdata/@app/@all”;
$method = ‘GET’;
$client = new APICall(false,’Trusted’);
$res = $client->callAPI($url,$method);
if(!empty($res)) {
$json_data = json_decode($res, true);
return $json_data;
}else {
return null;
}
}
function delTextDataGroup($group_name) {
$url=self::API_URL.”api/restful/v1/textdata/@app/$group_name/@self”;
$method = ‘DELETE’;
$client = new APICall(false,’Trusted’);
$client->callAPI($url,$method);
}
[/sourcecode]