Skip to content

Instantly share code, notes, and snippets.

@ritou
Created Mar 28, 2012
Embed
What would you like to do?
Sample Web Application to access Y!J Social APIs using PECL OAuth
<?php
// Request Token Endpoint
$req_url = 'https://auth.login.yahoo.co.jp/oauth/v2/get_request_token';
// AuthZ Endpoint
$authurl = 'https://auth.login.yahoo.co.jp/oauth/v2/request_auth';
// Access Token Endpoint
$acc_url = 'https://auth.login.yahoo.co.jp/oauth/v2/get_token';
// callback_url
$cbc_url = '(戻り先)';
// Consumerkey
$conskey = '(取得したConsumerKey)';
// Consumersecret
$conssec = '(取得したSecret)';
session_name("yjp_socialapis");
session_start();
if(isset($_GET['clear']) && $_GET['clear']==1){
$_SESSION=array();
// session_destroy();
}
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;
try {
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->enableDebug();
if(!isset($_GET['oauth_token']) && !$_SESSION['state'] || $_SESSION['state']==0 ) {
$request_token_info = $oauth->getRequestToken($req_url,$cbc_url);
$_SESSION['secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
exit;
} else if($_SESSION['state']==1) {
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_url,'',$_GET['oauth_verifier']);
$token_res = var_export($access_token_info, true);
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
$_SESSION['osh'] = $access_token_info['oauth_session_handle'];
$_SESSION['guid'] = $access_token_info['xoauth_yahoo_guid'];
}
if(isset($_GET['refresh']) && $_GET['refresh']==1){
// update Access Token
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_url, $_SESSION['osh']);
$token_res = var_export($access_token_info, true);
}
// API Access
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
// profile API
$profile_url = "http://social.yahooapis.jp/v1/user/".$_SESSION['guid']."/profile";
$api_params = array("format" => "json");
$oauth->fetch($profile_url, $api_params, OAUTH_HTTP_METHOD_GET );
$profile_res = var_export(json_decode($oauth->getLastResponse(), true), true);
// connections API
$connections_url = "http://social.yahooapis.jp/v1/user/".$_SESSION['guid']."/connections";
$api_params = array("format" => "json");
$oauth->fetch($connections_url, $api_params, OAUTH_HTTP_METHOD_GET );
$connections_res = var_export(json_decode($oauth->getLastResponse(), true), true);
// status API
$status_url = "http://social.yahooapis.jp/v1/user/".$_SESSION['guid']."/profile/status";
$api_params = array("format" => "json");
$oauth->fetch($status_url, $api_params, OAUTH_HTTP_METHOD_GET );
$status_res = var_export(json_decode($oauth->getLastResponse(), true), true);
// updates_connections API
$updates_connections_url = "http://social.yahooapis.jp/v1/user/".$_SESSION['guid']."/updates/connections";
$api_params = array("format" => "json");
$oauth->fetch($updates_connections_url, $api_params, OAUTH_HTTP_METHOD_GET );
$updates_connections_res = var_export(json_decode($oauth->getLastResponse(), true), true);
// updates_connections API
$updates_connections_url = "http://social.yahooapis.jp/v1/user/".$_SESSION['guid']."/updates/connections";
$api_params = array("format" => "json");
$oauth->fetch($updates_connections_url, $api_params, OAUTH_HTTP_METHOD_GET );
$updates_connections_res = var_export(json_decode($oauth->getLastResponse(), true), true);
} catch(OAuthException $E) {
print_r($E);
}
?>
<html>
<head>
<title>r-weblife Yahoo! JAPAN Social APIs Sample App</title>
</head>
<body>
<h1>PECL OAuth Library Sample with Yahoo! JAPAN Social APIs</h1>
<p>レスポンスをPHPの連想配列にしたものをdumpしています。</p>
<a href="./">reload</a>
<a href="./?refresh=1">Access Token Refresh</a>
<a href="./?clear=1">restart</a>
<h2>Access Token Response : </h2>
<pre><?php echo htmlspecialchars(@$token_res, ENT_QUOTES); ?></pre>
<h2>API Response : </h2>
<h3><a href="http://developer.yahoo.co.jp/webapi/social/profiles/v1/profile.html">ユーザーのプロフィール情報(大)</a></h3>
<pre><?php echo htmlspecialchars(@$profile_res, ENT_QUOTES); ?></pre>
<h3><a href="http://developer.yahoo.co.jp/webapi/social/profiles/v1/connections.html">ユーザーの友だち</a></h3>
<pre><?php echo htmlspecialchars(@$connections_res, ENT_QUOTES); ?></pre>
<h3><a href="http://developer.yahoo.co.jp/webapi/social/status/v1/status.html">ユーザーのひとこと取得</a></h3>
<pre><?php echo htmlspecialchars(@$status_res, ENT_QUOTES); ?></pre>
<h3><a href="http://developer.yahoo.co.jp/webapi/social/updates/v1/updatesconnections.html">ユーザーの友だちの近況</a></h3>
<pre><?php echo htmlspecialchars(@$updates_connections_res, ENT_QUOTES); ?></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment