【问题标题】:Facebook PHP SDK $uid = $facebook->getUser() always returns 0 [duplicate]Facebook PHP SDK $uid = $facebook->getUser() 总是返回 0 [重复]
【发布时间】:2013-10-03 02:35:40
【问题描述】:

我正在尝试将我们的数据库与 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'
    });
}

谢谢

EDIT 尝试仅使用 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 和密码 还有……
  • 尝试添加标题:header('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 和 getLoginUrl 来获取访问权限。
    • 尝试获取和设置访问令牌

【问题讨论】:

    标签: facebook facebook-graph-api facebook-javascript-sdk facebook-php-sdk facebook-oauth


    【解决方案1】:

    在 window.fbAsyncInit 中,我将 cookie 和 oauth 设置为 true,这很有效!

    cookie:true,    
    oauth : true.
    

    确保在你的 php 中你也有

    cookie' => true.
    

    这将使 php 代码与 JS 集成。

    【讨论】:

      【解决方案2】:

      并且您需要确保用户已登录:

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

      范围由permissions stated here定义

      之后你应该能够做到:

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

      这将获得一个与用户关联的数组,$user['id'] 可能是您想要存储的数组以及电子邮件地址等任何其他信息 - 请务必在scope 中请求email

      希望能解决问题!

      【讨论】:

      • 试过仍然得到 0 回来。我不希望使用 getloginurl 再次登录(因为我已经通过 JS 登录了),但是这样做我仍然得到 0。
      • 我已经用代码更新了问题
      • 使用 JavaScript 登录 Facebook 将使用户的浏览器可以访问 Facebook api,但不能访问您的服务器,这将进行 PHP 调用 - 我将为您做一些阅读并尝试解释如何结合与 Facebook 通信的两种方法,但我想你需要用 PHP 和 getLoginUrl 登录,然后将你的 $facebook-&gt;getAccessToken() 传递给你的 JavaScript。会做一些阅读并给你一个更简洁的答案,但现在你应该确保登录后你可以print_r($facebook-&gt;api('/me'));并检索你的信息
      • 同样在你的 PHP 代码中,$loginUrl = $facebook-&gt;getLoginUrl($params = array('scope' =&gt; user_birthday,read_stream,'redirect_uri' =&gt; 'http://xxxx/index.php?act=success')); 缺少引号,因为你只是在传递一个字符串,所以请尝试:$loginUrl = $facebook-&gt;getLoginUrl($params = array('scope' =&gt; 'user_birthday,read_stream','redirect_uri' =&gt; 'http://xxxx/index.php?act=success'));
      • 哦,最后一件事,不要在 Header() 之前 echo 因为它可能会阻止重定向
      猜你喜欢
      • 2013-02-16
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 2011-10-11
      • 2014-03-29
      • 2014-03-24
      相关资源
      最近更新 更多