$ user = $ facebook-> getUser()使用SDK 3.2.2返回0

时间:2012-12-08 06:29:54

标签: php facebook facebook-graph-api

我使用的是带有sdk但没有运气的facebook示例javascript代码 我正在使用最新的3.2.2 SDK

require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => '---------',
  'secret' => '----------',
));

// See if there is a user from a cookie
echo $user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

1 个答案:

答案 0 :(得分:0)

使用此代码:

header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => '---------',
  'secret' => '----------',
));
$querystr=http_build_query($_REQUEST);
// See if there is a user from a cookie
$user = $facebook->getUser();

if(isset($_REQUEST['code'])) {

            if(!empty($_REQUEST['code'])) {
setcookie("access_token", $_REQUEST['code'], time()+120);
            } 
            echo ("<script> top.location.href='<YOUR APP URL>'</script>");
            exit;
}

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}else {
$scope='email';

    $params = array(
            'redirect_uri' => 'YOUR SITE URL(WHere app is hosted)',
            'display' => 'page',
            'scope' => $scope
        );
    $loginurl = $facebook->getLoginUrl($params);

     echo ("<script> top.location.href='".$loginurl."'</script>");
     exit;
}