Clint C.
Clint C.

Reputation: 688

PHP SDK 3.1.1 and JS SDK getUser() returning 0

Well I just tried updating my script and I keep getting a return of 0 for getUser()

here are a couple snippets that i changed

old code

<?php

require_once("src/facebook.php");

class cfb
{
    public  $fb;
    public  $_fb_user;
    public  $_fb_details;
    public  $_fb_app_id         = '**************';
    private $_fb_app_secret     = '***************';
    private $_user;

    function __construct()
    {
        $this->fb = new Facebook(array(
        'appId'  => $this->_fb_app_id,
        'secret' => $this->_fb_app_secret,
        'cookie' => true,
        ));

        $this->_fb_user = $this->fb->getSession();

        $this->DB = new db();
        $this->_user = new user();
    }

    public function session_exists()
    {

        // see if there is a session stored, if so make sure the session is still good on facebooks end
        if($this->_fb_user) {

            // test if session is still good
            try
            {
                $me = $this->fb->api('/me');
            }
            catch(FacebookApiException $e){

                error_log($e);

            }

            if(!empty($me)) { 

                return true; 

            }

        } else { 

            return false;

        }
    }

new code

<?php

require_once("src/facebook.php");

class cfb
{
    public  $fb;
    public  $_fb_user;
    public  $_fb_details;
    public  $_fb_app_id         = '*****************';
    private $_fb_app_secret     = '********************';
    private $_user;

    function __construct()
    {
        $this->fb = new Facebook(array(
        'appId'  => $this->_fb_app_id,
        'secret' => $this->_fb_app_secret,
        ));

        $this->_fb_user = $this->fb->getUser();

        $this->DB = new db();
        $this->_user = new user();
    }

    public function session_exists()
    {

        // see if there is a session stored, if so make sure the session is still good on facebooks end
        if($this->_fb_user) {

            // test if session is still good
            try
            {
                $me = $this->fb->api('/me');
            }
            catch(FacebookApiException $e){

            echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';

            }

            if(!empty($me)) { 

                return true; 

            }

        } else { 

            return false;

        }
    }

here is my js code

<div id="fb-root"></div>
   <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId   : '<?php echo $fb->_fb_app_id; ?>',
          status  : true, // check login status
          cookie  : true, // enable cookies to allow the server to access the session
          xfbml   : true // parse XFBML
        });

        // whenever the user logs in, we refresh the page
        FB.Event.subscribe('auth.login', function(response) {
          window.location="<?php echo $fbredirect; ?>";
        });
      };

      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());


    </script>

I can't figure it out. was hoping it would be simple update but i guess not =/

Upvotes: 0

Views: 2727

Answers (1)

Cat Lee
Cat Lee

Reputation: 534

In your JS code, add oauth:true inside the FB.init function.

You can see the PHP SDK v.3.1.1 working with the JS SDK in the example here: https://developers.facebook.com/blog/post/534/

Upvotes: 2

Related Questions