PHP SDK 3.1.1 getUser() sometimes return 0

前端 未结 5 898
日久生厌
日久生厌 2020-12-17 21:32

This is driving me crazy >=(

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

Here is my code: require \'fbapi/facebook.php\';
$

5条回答
  •  伪装坚强ぢ
    2020-12-17 22:02

    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 :

     $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 "

    Logout"; $user_profile = $facebook->api('/me'); echo "

    Name : " . $user_profile['name']; echo "

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

提交回复
热议问题