Created
October 3, 2012 04:31
-
-
Save msng/3825006 to your computer and use it in GitHub Desktop.
Sample of Faceook Login using Facebook SDK for PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Facebook SDK for PHP の src/ にあるファイルを | |
//サーバ内の適当な場所にコピーしておく | |
require_once('php-sdk/facebook.php'); | |
$config = array( | |
'appId' => '[取得した App ID]', | |
'secret' => '[取得した App Secret]' | |
); | |
$facebook = new Facebook($config); | |
//ログイン済みの場合はユーザー情報を取得 | |
if ($facebook->getUser()) { | |
try { | |
$user = $facebook->api('/me','GET'); | |
} catch(FacebookApiException $e) { | |
//取得に失敗したら例外をキャッチしてエラーログに出力 | |
error_log($e->getType()); | |
error_log($e->getMessage()); | |
} | |
} | |
?> | |
<html> | |
<body> | |
<?php | |
if (isset($user)) { | |
//ログイン済みでユーザー情報が取れていれば表示 | |
echo '<pre>'; | |
print_r($user); | |
echo '</pre>'; | |
} else { | |
//未ログインならログイン URL を取得してリンクを出力 | |
$loginUrl = $facebook->getLoginUrl(); | |
echo '<a href="' . $loginUrl . '">Login with Facebook</a>'; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment