简体   繁体   English

facebook php sdk $ this-> getUser始终返回0

[英]facebook php sdk $this->getUser always returning 0

I have searched a lot for this question and found no particular solution for this question.I have seen many examples and so-called solutions but none of them have worked. 我在这个问题上进行了很多搜索,但没有找到具体的解决方案,我看到了很多示例和所谓的解决方案,但都没有用。 On top of that I am doing this watching a video by phpacademy and typing exactly as the video. 最重要的是,我正在通过phpacademy观看视频,并键入与视频完全相同的视频。

Before anyone mark this as a duplicate my only question is 在任何人将其标记为重复之前,我唯一的问题是

Is this because I am testing this in local machine? 这是因为我正在本地计算机上对此进行测试吗? Because the only difference between my setting and the video is that the tutorial is hosted live and I am testing this in my local machine. 因为我的设置和视频之间的唯一区别是该教程是实时托管的,因此我正在本地计算机上对此进行测试。

If this is the case?How can I test PHP facebook SDK in my local machine? 如果是这种情况,如何在我的本地计算机上测试PHP facebook SDK?

Here is my complete code this is my main controller 这是我完整的代码,这是我的主控制器

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Fbapp extends MX_Controller {


    function __construct() {
        $this->load->library('fbconnect');
        parent::__construct();
    }
    function index() {


        echo "<a href='" . base_url() . "fbapp/gotofb'>Go To Facebook</a>";
    }

    function gotofb(){

        $data = array(
            'redirect_uri' => base_url() . "fbapp/redirected",
            'scope' => "email,photo_upload"
        );
      $url =  $this->fbconnect->getLoginUrl($data);

     redirect($url);
    }

    function redirected(){
        $user_id= $this->fbconnect->getUser();
       echo $user_id;

    }
}

This is my library file that I have named "fbconnect.php",included sdk files inside libraries and incuded in library and I have created a facebook.php inside config which stores appId and secret which i have loaded as $config. 这是我的库文件,我已将其命名为“ fbconnect.php”,在库中包含了sdk文件,并插入了库中,并且在config内部创建了一个facebook.php,其中存储了appId和secret(已作为$ config加载)。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


require_once(APPPATH . 'libraries/facebook/src/facebook.php');
class Fbconnect extends Facebook{

    public $user = null;

    function __construct() {
        $ci =& get_instance();

        $ci->load->config("facebook", true);
        $config = $ci->config->item('facebook');

        parent::__construct($config);
    }

}

Steps: 1.i access my url .I click on the link. 步骤:1.i访问我的URL。单击链接。 2.it get me to the login page of facebook. 2.它使我进入facebook的登录页面。 I login. 我登录。 3. it redirects me to the redirect uri and prints 0. 3.它将我重定向到重定向uri并显示0。

Note:I am developing in codeigniter. 注意:我正在用Codeigniter开发。 Thanks in advance. 提前致谢。 Any help is super appreciated!! 任何帮助都非常感谢!!

Hi @developernaren Try using the following code in your library: 嗨@developernaren尝试在您的库中使用以下代码:

<?php
    if (!defined('BASEPATH')) exit('No direct script access allowed');

include(APPPATH.'libraries/facebook/src/facebook.php');

class Fb_connect extends Facebook {

    //declare protected variables
    protected $user     = NULL;
    protected $user_id  = FALSE;
            protected $friends      = NULL;

            //constructor method.
    public function __construct()
    {
                $CI = & get_instance();
                $CI->config->load("facebook",TRUE);
                $config = $CI->config->item('facebook');
                parent::__construct($config);
                $this->user_id = $this->getUser();
                if ($this->user_id) {
                    log_message('Info','Userid is fetched!');
                    try {
                        $this->user = $this->api('/me');
                        $this->friends = $this->api('/me/friends');
                    } catch (FacebookApiException $e) {
                        error_log($e);
                    }
                }
    }
            //Use this function to fetch the fb_uid. Function call: $this->fb_connect->user_id();
            public function user_id()
            {
                if(!empty($this->user_id))
                    return $this->user_id;
                else
                    return FALSE;
            }
          //Use this function to fetch the user details from fb. Function call: $this->fb_connect->user();
            public function user()
            {
                if(!empty($this->user))
                    return $this->user;
                else
                    return NULL;
            }}

I had same problem before, after days f searching i solved it in two ways if you are sure about all configuration ... I think you have to solutions to try: 1- host your application on web hosting server and try it with the domain your will get. 在经过数天的搜索之后,如果您确定所有配置,我都遇到了同样的问题,如果您确定所有配置,我都可以通过两种方式解决它...我认为您必须尝试以下解决方案:1-将应用程序托管在网络托管服务器上,并与域进行尝试你会得到的。 2- use javascript sdk to get the access_token then pass it to php skd, like this: 2-使用javascript sdk获取access_token,然后将其传递给php skd,如下所示:

              window.fbAsyncInit = function() {
            FB.init({
              appId      : 'appId', // App ID
              channelUrl : 'channel.html', // Channel File
              status     : true, // check login status
              cookie     : true, // enable cookies to allow the server to access the session
              xfbml      : true  // parse XFBML
            });
            // Additional init code here

            FB.Event.subscribe('auth.login', function(response) {
            var accessToken = response.authResponse.accessToken;
            window.location.href = "http://page_to_read_process_the _request?"accessToken="+accessToken;
         });

in php you only shoudl do like this: 在php中,您仅应这样做:

$facebook->setAccessToken($accessToken);
$facebook->getUser();

if this solved your problem: please do not forget to vote :D 如果这解决了您的问题:请不要忘记投票:D

You can try creating some kind of testing environment for local machine. 您可以尝试为本地计算机创建某种测试环境。

You need to understand how Facebook sends data to your application, and then you can create your environment. 您需要了解Facebook如何将数据发送到您的应用程序,然后才能创建环境。

  1. Facebook is sending request to your page (via POST), and your page is receiving this data, and you need to accept data and process it. Facebook正在通​​过POST向您的页面发送请求,并且您的页面正在接收此数据,您需要接受数据并对其进行处理。

  2. Try creating one online application (I'm using PHP, so I will write in that manner), and var_dump signed request. 尝试创建一个在线应用程序(我正在使用PHP,因此将以这种方式编写),并创建var_dump签名请求。

  3. After var_dump, you can see the signed request that is sent from the Facebook. var_dump之后,您可以看到从Facebook发送的已签名请求。

  4. Create PHP file with input field, where you can put your local application, and button that POST data to inputted page. 使用输入字段创建PHP文件,您可以在其中放置本地应用程序,然后将POST数据按入输入页面。 in the form, you will have hidden input named "signed_request" with data from step 2. 在表单中,您将隐藏名为“ signed_request”的输入,其中包含来自步骤2的数据。

  5. After you send data to your page, page will process signed request like it should do it online. 将数据发送到页面后,页面将像处理在线请求一样处理已签名的请求。

I know that this is a long process how to make it, but once you do it, you will have test environment, for all other applications in future. 我知道这是一个漫长的过程,但是一旦完成,您将拥有将来用于所有其他应用程序的测试环境。

Also, try creating several forms with several cases: 另外,尝试创建几种情况的几种形式:

  1. Facebook page tab not liked Facebook页面选项卡不喜欢
  2. Facebook page tab liked Facebook页面标签赞
  3. Facebook page tab with accepted permission box 带有接受权限框的Facebook页面选项卡
  4. etc... 等等...

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

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