简体   繁体   English

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

[英]Why is Facebook PHP SDK getUser always returning 0? Opencart

I am getting 0 as a result while accessing $this->fbconnect->getUser(); 访问$this->fbconnect->getUser();结果为0 in FB login in OpenCart2.1.x . 在FB中使用OpenCart2.1.x登录。

Below is the code, that i am using: 下面是我正在使用的代码:

  • In register controller, i am making URL as below: register控制器中,我正在制作如下URL:

     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') ) ); } 

and in register.tpl the URL is fine and i am being redirected to FB login page for authentication. register.tpl ,URL很好,我被重定向到FB登录页面进行身份验证。 but after login while returning back to my website, i am getting 0 for getUser() function. 但是登录后返回我的网站后,我的getUser()函数得到0

  • The Facebook Controller in catalog/controller/facebook/facebook.php the code is like below: catalog/controller/facebook/facebook.phpFacebook Controller的代码如下:

     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** 

Full code can be found here:: Click Here 完整的代码可以在这里找到:: 单击此处

My App Settings are: 我的应用程序设置是:

在此处输入图片说明

Here:: abc.com means that my actual website URL will be replaced here. 此处:: abc.com表示我的实际网站URL将在此处替换。

Please do not mark this question as DUPLICATE because i have already searched on SO and everywhere and nothing is working here for me. 请不要将此问题标记为DUPLICATE,因为我已经在SO上和所有地方进行了搜索,但这里没有任何工作。 So, please try to give the solution and that is i am posting question after searching for more than 1 day. 因此,请尝试提供解决方案,这就是我在搜索超过1天后发布问题。

Try this ;) 尝试这个 ;)

Replace 更换

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

with

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'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM