Skip to content

Instantly share code, notes, and snippets.

@kawahara
Created October 9, 2009 12:11
Show Gist options
  • Save kawahara/205965 to your computer and use it in GitHub Desktop.
Save kawahara/205965 to your computer and use it in GitHub Desktop.
<?php
// OpenSocial RESTful APIサンプル
// ライブラリとして http://oauth.googlecode.com/svn/code/php/ を利用しています。
include_once('oauth/OAuth.php');
define('CONSUMER_KEY', '#CONSUMER_KEY#');
define('CONSUMER_SECRET', '#CONSUMER_SECRET#');
define('BASE_URL', 'http://sns.example.com');
define('REQUEST_TOKEN_URL', BASE_URL.'/oauth/request_token');
define('AUTHORIZE_URL', BASE_URL.'/oauth/authorize');
define('ACCESS_TOKEN_URL', BASE_URL.'/oauth/access_token');
header('Content-Type: text/html; charset=utf8');
$consumer = new OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
if ($_GET['mode'] !== 'g'):
$request = OAuthRequest::from_consumer_and_token($consumer, null, 'POST', REQUEST_TOKEN_URL);
$request->set_parameter('oauth_callback', 'http://www.vm3/oauth.php?mode=g');
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, null);
$res = do_post($request->get_normalized_http_url(), $request->to_postdata());
$token = token_array_by_string($res);
header('Location: '.AUTHORIZE_URL.'?oauth_token='.$token['oauth_token']);
exit;
else:
$request = OAuthRequest::from_consumer_and_token($consumer, $_GET['oauth_token'], 'POST', ACCESS_TOKEN_URL, $_GET);
$token = new OAuthToken($_GET['oauth_token'], null);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, $token);
$res = do_post($request->get_normalized_http_url(), $request->to_postdata());
$tokenParam = token_array_by_string($res);
$token = new OAuthToken($tokenParam['oauth_token'], $tokenParam['oauth_token_secret']);
$request = OAuthRequest::from_consumer_and_token(
$consumer,
$tokenParam['oauth_token'],
'GET',
'http://openpne3.trunk.vm3/api.php/social/rest/people/@me/@self',
$tokenParam
);
$request->set_parameter('fields', 'birthday,age,languagesSpoken');
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, $token);
$res = do_get($request->get_normalized_http_url(), $request->to_postdata());
var_dump($res);
exit;
$object = json_decode($res);
$entry = $object->entry;
echo sprintf('%sの情報をとってきました。誕生日は%sのようです',$entry->displayName, $entry->birthday);
endif;
function do_post($uri, $data = '')
{
$h = curl_init();
curl_setopt($h, CURLOPT_URL, $uri);
curl_setopt($h, CURLOPT_POST, true);
curl_setopt($h, CURLOPT_POSTFIELDS, $data);
curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($h);
curl_close($h);
return $result;
}
function do_get($uri, $data = '')
{
$h = curl_init();
curl_setopt($h, CURLOPT_URL, $uri.'?'.$data);
curl_setopt($h, CURLOPT_POST, false);
curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($h);
curl_close($h);
return $result;
}
function token_array_by_string($string)
{
$result = array();
$params = explode('&', $string);
foreach ($params as $param)
{
$pieces = explode('=', $param);
$result[$pieces[0]] = $pieces[1];
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment