facebook-> getUser()返回0

时间:2014-01-26 19:11:34

标签: php facebook facebook-php-sdk

每当我尝试登录时,会打开一个提示基本权限,然后将其重定向到带有URL的redirect_uri

  

=“> HTTP://本地主机/演示/代码= AQDwzia3Wx1BktixF59jVHbm0ViGVJm8Xhb2tNZDyYreZh0KoSJhrSsJ8Aa2KX3gocwR0XNQjQz7ZlBh26_nBi-3iOMByhVO2cxwJ8maC4IHxBacfqXjzqIyBaZQbWKUUxPI6VBrqBgFXQasj7PEtmug7lt93dK4fmMC2A4i2dUYU-gSvzn0f0ZdB3eT_aSvgR1KoLCmQgLh3xix4H05QR6LCP9nLtQC4l9rMJW83kS0PNmWq0COZYvGfuX1R7519Fn3iXRB9F0MTsK1KQ_ulpK84PUCkuMu8et88Lln0ZwuzaPo0oERelkPWYnrrTKa-5W&安培;状态= ed66ea618d8076d9e72c15d9a65a6312# =

即使 facebook-> getUser()返回0

这是我的代码

<?php
require_once('php-sdk/facebook.php');
$facebook = new Facebook (array(
    'appId' => '1234',
    'secret' => '12313',
    'cookie' => true
    ));
?>

<html>
    <head> <title> Warming Up with FB API </title> </head>
    <body> <h1> Hello World </h1> </body>
</html>
<?php
$loginUrl = $facebook->getLoginUrl(array ( 
        'display' => 'popup',
        'redirect_uri' => 'http://localhost/demo'
        ));

$user = $facebook->getUser();
//echo $user. '</br>';
if ($user) {
    echo '<em>User Id: </em> '.$user;
} else {
    $loginUrl = $facebook->getLoginUrl(array ( 
        'display' => 'popup',
        'redirect_uri' => 'http://localhost/demo'
        ));
    echo '<a href = "'.$loginUrl.'">Login Here </a> ';
 }
?>

我知道这是一个非常微不足道的问题,但我有点坚持这个,无法继续前进。请建议做什么。

  

*的 更新 *

     

让App Domain为空解决了我的问题。

5 个答案:

答案 0 :(得分:11)

如果有人还在敲头,这就是现在的问题。我被雇来修理这个烂摊子!

  1. 在fb面板中检查应用领域,必须与应用所在的域匹配。

  2. 编辑base_facebook.php查找:

    公共函数getAccessToken(){

    if ($this->accessToken !== null) {
      // we've done this already and cached it.  Just return.
      return $this->accessToken;
    }
    
    // first establish access token to be the application
    // access token, in case we navigate to the /oauth/access_token
    // endpoint, where SOME access token is required.
    $getApplicationAccessToken = $this->getApplicationAccessToken();
    $this->setAccessToken($getApplicationAccessToken);
    $user_access_token = $this->getUserAccessToken();
    if ($user_access_token) {
      $this->setAccessToken($user_access_token);
    }
    return $this->accessToken;
    

    }

  3. 要::

      public function getAccessToken() {
    
        if ($this->accessToken !== null) {
          // we've done this already and cached it.  Just return.
          return $this->accessToken;
        }
    
        // first establish access token to be the application
        // access token, in case we navigate to the /oauth/access_token
        // endpoint, where SOME access token is required.
        $getApplicationAccessToken = $this->getApplicationAccessToken();
        $this->setAccessToken($getApplicationAccessToken);
        $user_access_token = $this->getUserAccessToken();
        if ($user_access_token) {
          //$this->setAccessToken($user_access_token);
        $this->accessToken = $user_access_token; //edit; msolution
        }
        return $this->accessToken;
      }
    
    1. 接下来找到函数:getAccessTokenFromCode()
    2. 找到这一行:

      parse_str($access_token_response, $response_params); 
      

      将其替换为:

      //parse_str($access_token_response, $response_params); //edit:: msolution;;
      $response_params = json_decode($access_token_response, true );
      

      评论原文并添加json_decode

      就是这样!

答案 1 :(得分:3)

如果用户已登录且仍然获得$ user = 0 我在不同的场合遇到过这个问题,但SDK问题并没有发生在每个人身上。所以我不确定这里出了什么问题。但我稍微深入了解并找到了解决方案,如下所述。

解决方案:在为此问题尝试多种解决方案后,这对我有用。

在base_facebook.php文件中,找到makeRequest()方法并检查以下Line。

$opts = self::$CURL_OPTS; 

紧接其后,添加此行

$opts[CURLOPT_SSL_VERIFYPEER] = false;  

更多细节可以在这里找到 - http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

答案 2 :(得分:2)

更改此

$loginUrl = $facebook->getLoginUrl(array ( 
        'display' => 'popup',
        'redirect_uri' => 'http://localhost/demo'
        ));

$loginUrl = $facebook->getLoginUrl(array ( 
        'display' => 'popup',
        'redirect_uri' => 'http://localhost/demo/index.php'
        ));

看看这是否有效!!

答案 3 :(得分:1)

几个小时后,我的解决方案是(假设我的网站是http://www.site123.com):

设置@ https://developers.facebook.com

  1. App Domain:site123.com(即使是空白也可以使用)
  2. 网站网址:http://site123.com重要提示:不是http://www.site123.com
  3. PHP代码 - fb_config.php

    //Declarations
    $app_id     = "{you AppID}";
    $app_secret = "{your App Secret}";
    $site_url   = "http://site123.com/receive_data_from_facebook.php";
    
    //New Facebook
    $facebook = new Facebook(array(
    'appId'     => $app_id,
    'secret'    => $app_secret,
    'cookie'    => TRUE, /* Optional */
    'oath'      => TRUE  /* Optional */
    ));
    
    //Login URL
    $loginUrl = $facebook->getLoginUrl(array(
        'scope'     => 'email,user_birthday',
        'redirect_uri'  => $site_url,
        ));
    
    //Get FacebookUserID
    $fbuser = $facebook->getUser();
    

    重要说明另外:我遇到了一些问题Firefox $ facebook-&gt; getUser()即使我的代码在Chrome上工作也是0。但在清理Firefox 缓存之后,它运行良好!

    希望它有所帮助。

答案 4 :(得分:0)

可能使用

$opts[CURLOPT_SSL_VERIFYPEER] = false;  after $opts = self::$CURL_OPTS; on file base_facebook.php.