无论发生什么情况,Facebook php sdk都无法获取用户id(我获取0)

无论发生什么情况,Facebook php sdk都无法获取用户id(我获取0),php,facebook,facebook-php-sdk,userid,Php,Facebook,Facebook Php Sdk,Userid,在这件事上我真的需要一些帮助 我想在这里创建一个与facebook页面链接的应用程序。到目前为止还不错。。 问题是我无法使用php sdk检索用户id,无论是什么 我也尝试过这个方法,但没有任何区别 require 'php-sdk/facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId

在这件事上我真的需要一些帮助

我想在这里创建一个与facebook页面链接的应用程序。到目前为止还不错。。 问题是我无法使用php sdk检索用户id,无论是什么

我也尝试过这个方法,但没有任何区别

require 'php-sdk/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId' => 'myappid',
  'secret' => 'mysecretid',
));

$theRequest = $facebook->getSignedRequest();
$user = $facebook->getUser();
var_dump($user);
无论我尝试了什么,都无法检索用户id

我尝试了保罗建议的链接

使用以下代码

if ($_REQUEST) {
  $signed_request = $_REQUEST['signed_request'];
} else {
  echo '$_REQUEST is empty';
}

function parse_signed_request($signed_request) {
  list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

  // decode the data
  $sig = base64_url_decode($encoded_sig);
  $data = json_decode(base64_url_decode($payload), true);

  return $data;
}

function base64_url_decode($input) {
  return base64_decode(strtr($input, '-_', '+/'));
}

$data = parse_signed_request($signed_request);
var_dump($data);
我得到一个“NULL”作为响应..:

第二次编辑。。我有这个密码

$information=parse_signed_请求($signed_请求,$secret); $oauth_令牌=$information[“oauth_令牌”]

var_dump($信息); var_转储($oauth_令牌)

$app\u id=“myappid”;
$canvas_page=”https://apps.facebook.com/app_namespace";
$auth_url=”http://www.facebook.com/dialog/oauth?client_id=" 
. $应用程序id。“&重定向_uri=”。urlencode($canvas\u页面)。“&scope=read\u stream,friends\u likes”;
$signed_request=$_request[“signed_request”];
列表($encoded_sig,$payload)=分解('.',$signed_请求,2);
$data=json_解码(base64_解码(strtr($payload,'-'','+/')),true);
if(空($oauth_-token)){echo(“top.location.href=”“$auth_-url。””);}
通过这种方式,我成功地获得了auth对话框,但在重定向中,我得到了一条配置错误的应用程序消息。如果我重新进入应用程序,那么我将访问所有必要的信息。令牌等。。 所以,如果我找到了一个解决方案,为什么在重定向应用程序中我得到了错误配置,我想我解决了我的问题?

首先,除非用户登录到您的应用程序,否则不会提供
user\u id
oauth\u令牌
。它只会在
user
键中提供有关用户的基本信息,该键是一个JSON对象,包含地区字符串、国家字符串和年龄对象。
因此,如果您想在这里获得
用户id
实现或测试用例,您可以执行以下操作

<?php
$params = array(
  'redirect_uri' => 'http://yourdomain.com/post_login_page'
);

$loginUrl = $facebook->getLoginUrl($params);
if(!$facebook->getUser()):
?>
<script>window.location.href = "<?php $loginUrl; ?>"</script>
<?php endif ?>

window.location.href=“”

我不知道谁会觉得这个有用还是没用。。 我认为如果facebook建议每个sdk更适用的离散实例,这将是一个好主意。。 在php sdk与facebook应用程序在页面选项卡上集成了好几个小时之后,facebook上的应用程序是毫无疑问的,我最终认为javascritp sdk会更好地满足我的目的,因为, a) 它不会将用户从我的页面重定向到身份验证 b) 它不要求应用程序也作为facebook应用程序存在(在我的例子中,我希望显示为页面标签) c) 它对所有内容进行了一种覆盖,打开“身份验证”对话框,然后返回到我的页面,提供所有有用的信息

从几个fb源复制代码对我来说很有效

    FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire
    uid = response.authResponse.userID;
    //alert(uid);
    var accessToken = response.authResponse.accessToken;
  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app
             FB.login(function(response) {
                if (response.authResponse) {
                uid = response.authResponse.userID;
                console.log('Welcome!  Fetching your information.... ');
                FB.api('/me', function(response) {
                    });
            } else {
            }
            });
        } else {
    // the user isn't logged in to Facebook.
        }
 });

 // Additional initialization code such as adding Event Listeners goes here

  };

用户是否授权了您的应用程序?我目前以开发人员的身份独自使用它,所以我想我是默认授权的?我不知道您是否读过以下内容:但对于页面应用程序,您必须:在您的页面上注册应用程序,仅在加载页面时尝试解码已签名的\u请求(基本上,在点击页面上的图标以访问您的页面应用程序后)我实际上没有读到这一点,但我仍然得到一个空值…我将在我的第一篇文章中提供编辑代码。@nikolas您未经defaulti授权我使用$params=array('scope'=>'read\u stream,friends\u likes',);我什么都没有得到授权什么都没有……我不明白发生了什么事。$loginUrl=$facebook->getLoginUrl($params);var_dump($loginUrl);@nikolas您需要重定向到登录URL以进行happeni的身份验证我成功地获得了已签名的请求,但登录..和用户id没有成功..s您在
$theRequest
中得到了什么?我得到了类似的东西..缺少很多参数..我试图获得该脚本,最终获得了身份验证对话框,返回后我得到了错误的身份验证guered应用程序!!!:php sdk应该解决问题吗??:(
    FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire
    uid = response.authResponse.userID;
    //alert(uid);
    var accessToken = response.authResponse.accessToken;
  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app
             FB.login(function(response) {
                if (response.authResponse) {
                uid = response.authResponse.userID;
                console.log('Welcome!  Fetching your information.... ');
                FB.api('/me', function(response) {
                    });
            } else {
            }
            });
        } else {
    // the user isn't logged in to Facebook.
        }
 });

 // Additional initialization code such as adding Event Listeners goes here

  };