为什么Facebook PHP SDK getUser总是返回0? Opencart的

时间:2016-05-06 10:03:15

标签: php facebook facebook-php-sdk opencart2.x

我在 OpenCart2.1.x 的FB登录中访问0时得到$this->fbconnect->getUser();

以下是我正在使用的代码:

  • register控制器中,我正在制作如下网址:

    if(!$this->customer->isLogged()){
               if(!isset($this->fbconnect)){            
        require_once(DIR_SYSTEM . 'facebook/facebook.php');
        $this->fbconnect = new Facebook(array(
            'appId'  => $this->config->get('facebook_apikey'),
            'secret' => $this->config->get('facebook_apisecret'),
             'redirect_uri'  => $this->url->link('facebook/facebook', '', 'SSL'),
             'cookie' => true,
             'domain'=>'abc.com'
        ));
       }
    
       $data['facebook_url'] = $this->fbconnect->getLoginUrl(
        array(
            'scope' => 'public_profile, email',
            'redirect_uri'  => $this->url->link('facebook/facebook', '', 'SSL')
        )
       );
    }
    

并且在register.tpl中,网址很好,我正被重定向到FB登录页面进行身份验证。但在返回我的网站后登录后,我0函数获得了getUser()

  • Facebook中的catalog/controller/facebook/facebook.php控制器代码如下:

    if(!isset($this->fbconnect)){           
        require_once(DIR_SYSTEM . 'facebook/facebook.php');
    
        $this->fbconnect = new Facebook(array(
            'appId'  => $this->config->get('fbconnect_apikey'),
            'secret' => $this->config->get('fbconnect_apisecret'),
             'redirect_uri' => $this->url->link('facebook/facebook', '', 'SSL'),
        ));
    }
    
    $_SERVER_CLEANED = $_SERVER;
    $_SERVER = $this->clean_decode($_SERVER);
    
    echo $fbuser = $this->fbconnect->getUser();  **0**
    

完整代码可以在这里找到:: Click Here

我的应用设置是:

enter image description here

此处:: abc.com表示我的实际网站网址将在此处替换。

  

请不要将此问题标记为 DUPLICATE ,因为我已经搜索了SO和所有地方,这里没有任何内容可供我使用。所以,请尝试给出解决方案,这是我在搜索超过1天后发布问题。

1 个答案:

答案 0 :(得分:1)

试试这个;)

替换

echo $fbuser = $this->fbconnect->getUser();

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $this->fbconnect->get('/me?fields=id,name', '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$user = $response->getGraphUser();

echo 'Id: ' . $user['id'];
echo 'Name: ' . $user['name'];