Why is Facebook PHP SDK getUser always returning 0? Why is Facebook PHP SDK getUser always returning 0? php php

Why is Facebook PHP SDK getUser always returning 0?


I had the same problem and I figured it out that is because SDK uses the variable $_REQUEST and in my environment is not true that is merged with $_GET, $_POST and $_COOKIE variables.

I think it depends on the PHP version and that is why someone made it work by enabling cookies.

I found this code in base_facebook.php:

protected function getCode() {    if (isset($_REQUEST['code'])) {        if ($this->state !== null &&                isset($_REQUEST['state']) &&                $this->state === $_REQUEST['state']) {            // CSRF state has done its job, so clear it            $this->state = null;            $this->clearPersistentData('state');            return $_REQUEST['code'];        } else {            self::errorLog('CSRF state token does not match one provided.');            return false;        }    }    return false;}

And I modified it as you can see below by creating $server_info variable.

protected function getCode() {    $server_info = array_merge($_GET, $_POST, $_COOKIE);    if (isset($server_info['code'])) {        if ($this->state !== null &&                isset($server_info['state']) &&                $this->state === $server_info['state']) {            // CSRF state has done its job, so clear it            $this->state = null;            $this->clearPersistentData('state');            return $server_info['code'];        } else {            self::errorLog('CSRF state token does not match one provided.');            return false;        }    }    return false;}


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 :

<?phpinclude '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.anujkumar.com' );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'))}");}?>