简体   繁体   中英

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.

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?

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.

<?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. 2.it get me to the login page of facebook. I login. 3. it redirects me to the redirect uri and prints 0.

Note:I am developing in codeigniter. Thanks in advance. Any help is super appreciated!!

Hi @developernaren Try using the following code in your library:

<?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. 2- use javascript sdk to get the access_token then pass it to php skd, like this:

              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:

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

if this solved your problem: please do not forget to vote :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.

  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.

  2. Try creating one online application (I'm using PHP, so I will write in that manner), and var_dump signed request.

  3. After var_dump, you can see the signed request that is sent from the Facebook.

  4. Create PHP file with input field, where you can put your local application, and button that POST data to inputted page. in the form, you will have hidden input named "signed_request" with data from step 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
  2. Facebook page tab liked
  3. Facebook page tab with accepted permission box
  4. etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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