开发者

PHP SDK 3.1.1 getUser() sometimes return 0

This is driving me crazy >=(

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

Here is my cod开发者_如何学Goe: 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???


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 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'))}");
}

?>


You just input php header to

header('P3P: CP="NOI ADM DEV COM NAV OUR STP"');


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.. :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜