Facebook PHP-SDK getUser() always returns 0

11,135

Solution 1

Solution:

After messing with a lot of suggestions I discovered the answer is both simple and stupid. The " ' " in the following code are not true single quotation marks (or whatever they are called). Instead " ‘ " is used, which is something different thats not recognized.

Basically ‘ != '

The original code:

$config[‘appId’] = 'x0x2x1x9xx3x98x';
$config[‘secret’] = 'xxx79xf7x6xxx5xxxfxxxc5bx46xfxxx';
$config[‘fileUpload’] = false; // optional

The fixed code:

$config['appId'] = 'x0x2x1x9xx3x98x';
$config['secret'] = 'xxx79xf7x6xxx5xxxfxxxc5bx46xfxxx';
$config['fileUpload'] = false; // optional

All I did was replace the " ’ " with " ' " which caused my text editor to recognize the names as quoted strings. I uploaded it and it worked! I think it was an encoding problem somewhere along the line that changed the character, which makes sense. It means the FB team did originally write good code, but that people are having valid problems with it.

Solution 2

If getUser() returns 0 than your application has not been granted permission by the user viewing.

Even if you are the developer of the application it will treat you the same, it would be hard to test much if your account behaved special.

With that said, it is very easy.

If getUser() returns 0, redirect the user to the login url. Assuming they accept you will then be able to properly use getUser().

See: https://github.com/facebook/php-sdk/blob/master/examples/example.php for a good example

Solution 3

Here's what i did..

Call getUser() and if the result is 0 redirect to the login url or getLoginUrl(). Once it visit the app it will have the value for GetUser along with the signed request from Facebook.

If your first timer using the app visiting getLoginUrl will bring you to the App Permission request which you have to accept it in order to use the Facebook app.

Share:
11,135
Yuvals
Author by

Yuvals

Updated on June 11, 2022

Comments

  • Yuvals
    Yuvals almost 2 years

    Possible Duplicate:
    Why is Facebook PHP SDK getUser always returning 0?

    I am using the new php SDK to create a very simple app. When I try $facebook->getUser(), it always returns 0 (claiming that I'm not logged in to FB, howeven I'm already INSIDE Facebook..

    It only works when I'm loggin in to the app from facebook.. any idea how to solve this?

    Thanks, Yuval