Skip to content

Instantly share code, notes, and snippets.

@co3k
Created September 12, 2009 14:30
Show Gist options
  • Save co3k/185853 to your computer and use it in GitHub Desktop.
Save co3k/185853 to your computer and use it in GitHub Desktop.
<?php
require_once 'OAuth.php';
require_once 'OpenPNEOAuth.class.php';
// config
define('CONSUMER_KEY', ''); // コンシューマキー
define('CONSUMER_SECRET', ''); // コンシューマシークレット
define('BASE_URL', 'http://sns.example.com/'); // SNS の URL (最後は / で終わる必要があります)
define('SCRIPT_URL', 'http://externals.example.com/oauth_sample.php'); // このスクリプトの URL
// 初回のアクセス
if ('post' !== strtolower($_SERVER['REQUEST_METHOD']) && 'result' !== $_GET['mode'])
{
$content = <<<CNT
<p>以下のボタンを押下し、 ID 1 のメンバーの情報を取得します。</p>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input type="submit" value="情報取得" />
</form>
CNT;
}
// OAuth による認可をおこなうためにサービスプロバイダにリダイレクト
elseif ('post' === strtolower($_SERVER['REQUEST_METHOD']))
{
$token = OpenPNEOAuth::getInstance(BASE_URL, CONSUMER_KEY, CONSUMER_SECRET)
->getRequestToken(SCRIPT_URL.'?mode=result'); // リクエストトークンの取得
header('Location: '.OpenPNEOAuth::getInstance()->getAuthorizeUrl($token)); // 認可用 URL を取得し、リダイレクト
exit;
}
// アクセストークンを取得し、 API アクセス
else
{
$token = OpenPNEOAuth::getInstance(BASE_URL, CONSUMER_KEY, CONSUMER_SECRET)
->getAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']);
$result = OpenPNEOAuth::getInstance()->doOAuthGet(BASE_URL.'api.php/feeds/member/1', $token);
if ($result)
{
$xml = simplexml_load_string($result, null, LIBXML_NOCDATA);
$content = <<<CNT
<dl>
<dt>ニックネーム</dt>
<dd>{$xml->title}</dd>
CNT;
if (!empty($xml->link[3]['href']))
{
$content .= <<<CNT
<dt>写真</dt>
<dd><img src="{$xml->link[3]['href']}" alt="{$xml->title}" /></dd>
CNT;
}
$content .= <<<CNT
</dl>
<ul>
<li><a href="{$xml->link[1]['href']}">{$xml->title} さんのプロフィール</a></li>
</ul>
CNT;
}
else
{
$content = '<p>メンバーの情報が正常に取得できませんでした</p>';
}
}
?>
<?php header('Content-Type: text/html; charset=utf8'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>メンバー情報の取得</title>
</head>
<body>
<?php echo $content ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment