我试图只使用php sdk登录facebook应用程序。 为什么php身份验证不起作用。 - >的getUser();始终返回零。
<?php
// Provides access to app specific values such as your app id and app secret.
// Defined in 'AppInfo.php'
require_once('AppInfo.php');
// Enforce https on production
if (substr(AppInfo::getUrl(), 0, 8) != 'https://' && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
header('Location: https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit();
}
// This provides access to helper functions defined in 'utils.php'
require_once('utils.php');
require_once('sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'sharedSession' => true,
'trustForwarded' => true,
'cookie' => true
));
$auth_url = $facebook->getLoginUrl(array(
'scope' => 'email,publish_stream'
));
$user = $facebook->getUser();
if (empty($user)) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
echo ("Welcome User: " . $user);
}
?>
答案 0 :(得分:2)
您没有将acces_token设置为facebook对象。当用户在身份验证后重定向到您的网页时,您应该在$facebook
对象中设置用户access_token,然后调用$facebook->getUser()
你可以这样做:
if(isset($_GET['code'])){
$access_token = $facebook->getAccessTokenFromCode($_GET['code']);
$facebook->setAccessToken($access_token);
}
$user = $facebook->getUser();