PHP SDK 3.1.1 getUser() sometimes return 0
Asked Answered
T

5

9

This is driving me crazy >=(

$facebook->getUser() works well sometimes, but sometimes returns 0

Here is my code: require 'fbapi/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxxxx',
));
$user = $facebook->getUser();

the appId and secret are the correct ones.

Which could be the reason that getUser sometimes it returns 0???

Toreador answered 29/8, 2011 at 6:32 Comment(4)
Will this topic help you solve your problem? : Why is Facebook PHP SDK getUser always returning 0?Dell
@Janis It shouldn't have anything to do with the JS SDK. He's not doing anything here that would require it.Alexandros
Are you using Safari? Are you using the JS SDK in conjunction with the PHP SDK?Roselynroseman
+1 @Mike, people are reporting that when using the PHP-SDK along with the JS-SDK the cookies will not get set the first reload. you need another reload to have the $user set (I confirm this issue on FF)Pisces
A
1

It's more than likely something on Facebook's end (devs have gone through this before a while back). If $user returns null or 0, simply reroute them to the login url (which you should have). The liklihood of it returning 0 again is minimal (unless there's a bug on their end or there's more to your code than what you've posted).

Alexandros answered 29/8, 2011 at 6:36 Comment(10)
Thanks Philips but if I do that, the page loops infinitely... :( Here is the code with reload and loops.. ` if ($user) { try { $user_profile = $facebook->api('/me');<br> } catch (FacebookApiException $e) {<br> echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; } }else{ $log = $facebook->getLoginUrl(array('canvas' => 1,'fbconnect' => 0)); echo("<script> top.location.href='".$log."'</script>"); } `Toreador
@Toreador Are you absolutely certain you are using the latest PHP SDK?Alexandros
@Toreador is it perhaps your application settings? Are you upgraded to OAuth 2.0?Alexandros
Yes @philips, I upgraded to OAuth 2.0 and I use latest PHP SDK downloaded today.. <br> Sometimes the getUser works fine, and sometimes return 0.. =/ I have no idea, becouse I did not change any code line..Toreador
@Toreador If it works on occasion, it leads me to believe its an intermittent problem on Facebook end. This happened with the older PHP SDK (before 3.0) where it would sometimes return 0.Alexandros
I will download PHP Skd again just to try.. Thanks a lot @PhilipToreador
@Toreador are you using the JS SDK as well?Alexandros
Thanks @Phillips, yes I am using JS SDK too. I am testing againg now.Toreador
I have users running into this, and I use the JS SDK and PHP SDK. The crazy thing is I can't reproduce at all on my end, and it seems to always be the same users running into it. I get them to clear their cache and it still happens. Very strange.Grouse
Do you happen to have cookie:true enabled with your JS SDK? Turning that off might fix it (though you'd lose access to cookies of course).Alexandros
S
1

I ran into similar problem. $facebook->getUser() was returning 0 and sometimes it returned valid user id when user wasn't actually logged in, resulting in Fatal Oauth error when I tried to make graph api calls. I finally solved this problem. I don't know if it is the right way but it works. Here is the code :

<?php
include 'includes/php/facebook.php';
$app_id = "APP_ID";
$app_secret = "SECRET_KEY";
$facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $app_secret,
    'cookie' => true
));

$user = $facebook->getUser();

if ($user <> '0' && $user <> '') { /*if valid user id i.e. neither 0 nor blank nor null*/
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) { /*sometimes it shows user id even if user in not logged in and it results in Oauth exception. In this case we will set it back to 0.*/
error_log($e);
$user = '0';
}
}
if ($user <> '0' && $user <> '') { /*So now we will have a valid user id with a valid oauth access token and so the code will work fine.*/
echo "UserId : " . $user;

$params = array( 'next' => 'http://www.quickbite.co.in' );
echo "<p><a href='". $facebook->getLogoutUrl($params) . "'>Logout</a>";

$user_profile = $facebook->api('/me');
echo "<p>Name : " . $user_profile['name'];
echo "<p>";
print_r($user_profile);

} else {/*If user id isn't present just redirect it to login url*/
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'email,offline_access'))}");
}

?>
Sew answered 11/11, 2011 at 8:34 Comment(0)
C
1

You just input php header to

header('P3P: CP="NOI ADM DEV COM NAV OUR STP"');
Charlacharlady answered 9/7, 2012 at 11:20 Comment(0)
T
0

So i was struggling with the same problem, getUser() always returned 0. Did not make any sense at all. NginX error logs was showing the following error 'CSRF state token does not match one provided'. Then I started debugging the sdk and narrowed it down to getAccessTokenFromCode() not able to get the right accesstoken from the Facebook servers, something to do with SSL certificates.

The solution at the below mentioned link solved the problem for me.

https://github.com/facebook/php-sdk/issues/272

Also you may need to copy the 'fb_ca_chain_bundle.crt' file bundled with the sdk to folder where your facebook class file is located. i did.

I hope this makes sense.

Tardigrade answered 30/9, 2011 at 7:51 Comment(2)
Do you by any chance remember what that issue was? It's been taken off github since...Vaughn
fb_ca_chain_bundle.crt is already there but I'm having difficulties again but getuser after facebook callback.Knitting
E
0

I found a simple solution to this problem.. this happens when facebook class is not able to set or add session variable on your server..

In my code i used session_write_close() before including facebook classes because of which i got this error.. "CSRF state token does not match one provided."..

I then moved session_write_close() at the end of the code and finally it worked.. :)

Exuberate answered 13/1, 2012 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.