Facebook的PHP SDK $ uid = $ facebook-> getUser()总是返回0

问题描述:

我正在尝试将我们的数据库与Facebook API集成在一起,真的是新的,所以我希望得到一些帮助。

我总是得到0

使用

$uid = $facebook->getUser() 

我看看类似的问题,但并没有找到相关的解决方案。

PHP

if($_REQUEST['action']=="signup" && $_REQUEST['auth']=="facebook" && !empty ($_REQUEST['auth']) && !empty ($_REQUEST['action'])) 
{ 

require_once('libs/facebook/src/facebook.php'); 

$config = array(); 
$config['appId'] = 'xxxx'; 
$config['secret'] = 'xxxxxx'; 

$facebook = new Facebook($config); 
$uid = $facebook->getUser(); 
echo $uid; 
} 

//else {} normal sign up here with all the error checking 

HTML

<div class="fb-login-class"> 
     <div class="fb-login-button" autologoutlink="true" scope="publish_stream" perms="email,user_birthday" data-show-faces="false" 
     data-width="200" data-max-rows="1"></div> 
</div> 

JAVASCRIPT/JQUERY

window.fbAsyncInit = function() { 
    // init the FB JS SDK 
    FB.init({ 
     appId  : 'XXXX,      // App ID from the app dashboard 
     channelUrl : '//WWW.XXXX.com', // Channel file for x-domain comms 
     status  : true,         // Check Facebook Login status 
     xfbml  : true         // Look for social plugins on the page 
    }); 



    // Additional initialization code such as adding Event Listeners goes here 
    // will be handled. 
    FB.Event.subscribe('auth.authResponseChange', function(response) { 
    // Here we specify what we do with the response anytime this event occurs. 
    if (response.status === 'connected') { 


     loginfunction(); 
    } else if (response.status === 'not_authorized') { 

     FB.login(function(response) {},{scope: 'email'}); 
    } else { 

     FB.login(function(response) {},{scope: 'email'}); 
    } 
    }); 
    }; 

(function(d, s, id) { 
    var js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) return; 
    js = d.createElement(s); js.id = id; 
    js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=XXXXX"; 
    fjs.parentNode.insertBefore(js, fjs); 
}(document, 'script', 'facebook-jssdk')); 


function loginfunction() { 
    console.log('Welcome! Fetching your information.... '); 
    FB.api('/me', function (response) { 
     console.log(response); 
/*I can see all the data here and the button also changes so I know that I am logged in */ 
     var data = 'action=signup&username=' + response.username + '&firstname=' + response.first_name + '&gender=' + response.gender + '&lastname=' + response.last_name + '&email=' + response.email + '&dob=' + "xxxx" + '&auth=facebook'; 
/*What's the point in passing all these parameters through ? only need to pass &auth=facebook and action=signup I guess the rest can be manipulated anyway so what's the point 
need to get them from PHP SDK (server side ajax call) */ 

     $.ajax({ 
      type: "POST", 
      url: "actions/do_signup.php", 
      data: data, 
      beforeSend: function (html) {}, 
      success: function (html) { 
       if (html == "success") { 
        window.location.replace("index.php"); 
       } else {} 
      } 
     }); 
    }, { 
     scope: 'email' 
    }); 
} 

感谢

编辑试图只用PHP仍然未能

require('facebook.php'); 
$facebook = new Facebook(array (
    'appId' => 'xxx', 
    'secret' => 'xxxx', 
    'cookie' => true 
) 
); 


if ($_REQUEST['act']=="fb") 
{ 
//$next_url = $facebook->getLoginStatusUrl($params); 

if ($facebook->getUser()) { 
     $userProfile = $facebook->api('/me'); 
     $uid = $facebook->getUser(); 
     // do logic 
     echo $uid ; 
     echo "ok"; 
    } else { 
    echo "problem"; 
     $loginUrl = $facebook->getLoginUrl($params = array('scope' => user_birthday,read_stream,'redirect_uri' => 'http://xxxx/index.php?act=success')); 
     header('Location:' . $loginUrl); 

    } 
} 
else if ($_REQUEST['act']=="success") 
{ 
    try{ 
     $_SESSION['fbAT'] = $facebook->getAccessToken(); 
    }catch(Exception $e){ 
     trigger_error("FB AccessToken: " . $e->getMessage()); 
    }  
$facebook->setAccessToken($_SESSION['fbAT']); 

if ($facebook->getUser()) { 
     $uid = $facebook->getUser(); 
     $userProfile = $facebook->api('/me'); 

     // do logic 
     echo $uid ; 
     echo $userProfile; 
     echo "ok"; 
    } else { 
    $uid = $facebook->getUser(); 
    echo $uid ; 
    echo "still problem"; 


    } 

,我已经尝试了一些步骤:

  • 沙盒模式被禁止,我已经确认了应用程序ID和秘密 以及...
  • 尝试添加标题:标题('P3P :CP =“IDC DSP COR ADM DEVI TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT”');
  • 新增的cookie” =>真
  • 尝试从一个静态页面调用它不是通过Ajax还是0
  • 只有网站Facebook的基本选项卡
  • 下的高级应用型下使用设置为网络
  • 下权限默认活动是公众
  • 确认在fb_ca_chain_bundle.crt是在同一目录是facebook.php也尝试使用不同的目录(我的PHP文件相同)
    • 尝试仅PHP和getLog inUrl具有额外的访问权限。
    • 试图获取和设置令牌

在window.fbAsyncInit我已经设置cookie和oauth为true,这工作!

cookie:true,  
oauth : true. 

确保在你的PHP你也有

cookie' => true. 

这将使PHP代码与JS给integrare。

访问你需要确保用户在登录时:

$loginUrl = $facebook->getLoginUrl(array('scope' => 'read_stream,etc..')); 

的范围由permissions stated here

定义之后,你应该可以做到:

$user = $facebook->api("/me"); 

这将获得与用户相关联的阵列,$user['id']可能是你要存储一个以及像电子邮件地址的任何其他信息 - 请务必在scope

希望能为email要求,解决问题!

+0

试过仍然回0。我不希望需要使用getloginurl重新登录(因为我已经通过JS登录),但是这样做我仍然回到0。 – Athanatos

+0

我用代码更新了这个问题 – Athanatos

+0

使用JavaScript登录到Facebook会让用户浏览器访问Facebook api,但不能访问您的服务器,这将使PHP调用 - 我将为您做一些阅读并尝试解释如何结合与Facebook沟通的两种方法,但是我认为你需要使用PHP和'getLoginUrl'登录,然后将'$ facebook-> getAccessToken()'传递给你的JavaScript。将做一些阅读,并给你一个更简洁的答案,但现在你应该确保在登录后你可以'print_r($ facebook-> api('/ me'));'并检索你的信息 – GroovyCarrot