Kam*_*al 2 php facebook facebook-graph-api facebook-php-sdk
我想让用户通过Facebook登录.我正在使用此代码:
<?php
require 'include/facebook.php';
require 'include/fbconfig.php';
require 'include/user.php';
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true
));
$session = $facebook->getSession();
if (!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try {
$uid = $facebook->getUser();
$user = $facebook->api('/me');
} catch (Exception $e) {
}
if (!empty($user)) {
# User info ok? Let's print it (Here we will be adding the login and registering routines)
echo '<pre>';
print_r($user);
echo '</pre><br/>';
$username = $user['name'];
$user = new User();
// check user method insert login information to my database and return the result
$userdata = $user->checkUser($uid, 'facebook', $username);
if(!empty($userdata)){
session_start();
$_SESSION['id'] = $userdata['id'];
$_SESSION['oauth_id'] = $uid;
$_SESSION['username'] = $userdata['username'];
$_SESSION['oauth_provider'] = $userdata['oauth_provider'];
header("Location: test.php");
}
} else {
# For testing purposes, if there was an error, let's kill the script
die("There was an error.");
}
} else {
# There's no active session, let's generate one
$params = array(
'scope' => 'email'
);
$login_url = $facebook->getLoginUrl($params);
header("Location: " . $login_url);
}
?>
Run Code Online (Sandbox Code Playgroud)
当我运行此页面时,它会将我重定向到facebook主页(我的帐户).
请问,有什么想法?
你的帖子没有太多的上下文来解释代码部分; 请更清楚地解释您的情景.
您使用的是最新的PHP SDK吗?从这里获取最新的SDK ,然后再试一次..
[EDITED]
替换所有的引用getSession()与 getUser()
例:
$session = $facebook->getSession();
Run Code Online (Sandbox Code Playgroud)
它应该说:
$session = $facebook->getUser();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3919 次 |
| 最近记录: |