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???
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 :
You just input php header to
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 togetAccessTokenFromCode()
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.
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).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.. :)