Facebook getUser() always returns 0 [PHP SDK 3.1.1

2019-02-27 12:26发布

问题:

This question already has an answer here:

  • Why is Facebook PHP SDK getUser always returning 0? 26 answers

fb.php:

ini_set('display_errors','On');
error_reporting(E_ALL);
require_once 'facebook/src/facebook.php';

$facebook = new Facebook( array (
    'appId'  => '****',
    'secret' => '****',
    'cookie' => true
  )
);

$user = $facebook->getUser();

if ($user) {
    var_dump($user);
  $logoutUrl = $facebook->getLogoutUrl();

  print '<a href="'.$logoutUrl.'">Logout</a>';
} 
else {
    $loginUrl = $facebook->getLoginUrl();
  print '<a href="'.$loginUrl.'">Login</a>';
}

This is my simple code; When the link is clicked, and accept the permission box, then I'm redirected to the script, and get this params: http://mydomain.com/fb.php?state=82e166c6a85fd3b44ad655851f885b73&code=AQCMo6L5z-K5juFl3cojl80KllKQ8JE2iKMaOFSmHhHgAFd_V-BfD8YZwbiKQasNuHSbcc4St-9B2gggwEMygn223u8BVK9zXKctEui-y20rcmydx-uf65sw5v6o4XN3k9Lnvtg0jg7h1C9XGC3S1sW32X65CI6kjbe7GXHICpMsT833wDxG1voS7enq9uYm1MGrp83ceTmKvoKGyn_iYby0#_=_

After these issues, I found this link suddenly, getUser became to return 0.(PHP 3.1.1 SDK), but doesn't works for me.

I'm using the PHP Facebook SDK Version 3.1.1.

¿Somebody know whats happening here?

回答1:

Please recheck your settings again including appId, secret and make sure Sandbox Mode is disabled on your apps dashboard.

I had the same problem after changing the domain of my site. Altough I properly changed the request_uri parameter and updated my app settings with the new domain, the error kept showing up. Then I realized that the ID and the SECRET ID of my Facebook APP had automatically changed with no warning!! The whole thing started working again using the new ID.

Some experts said they also faced same problem as you because of sandbox mode. From the Facebook Developers Apps page, make sure that the Sandbox Mode is disabled.

Another matter is permission scope you can set as following:

$pram = array(
           'scope' => 'user_birthday,read_stream',
           'redirect_uri' => 'http://domain.com/some_where_else/'
        )
$loginUrl = $this->facebook->getLoginUrl($pram);

may be it will help you. thanks.