我将系统从facebook sdk3.2 升级到 sdk4 。
以前,我们可以传递GET参数,例如:
// page where user will press login button (sdk3)
Facebook::getFBInstance($facebook);
$loginParams = array(
'redirect_uri' => 'http://www.mywebsite.com/requesthandler.php?destiny='.urlencode('http://www.mywebsite.com/some_other_page.php'),
'scope' => FACEBOOK_PERMISSION_SCOPE
);
?>
<a href="<?=$facebook->getLoginUrl($loginParams);?>">Login </a>
但是在SDK 4中,帮助器的第一个代码返回错误但第二个代码工作。我已经按照Facebook开发者页面上的所有步骤进行了操作。
// page where user will press login button (sdk4)
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSession;
FacebookSession::setDefaultApplication( FACEBOOK_API_ID, FACEBOOK_API_SECRET );
// this code returns error
$helper = new FacebookRedirectLoginHelper( 'http://www.mywebsite.com/
requesthandler.php?destiny='.urlencode('http://www.mywebsite.com/some_other_page.php') )
// wheareas this one works
$helper = new FacebookRedirectLoginHelper( 'http://www.mywebsite.com/
requesthandler.php' )
?>
<a href="<?=$helper->getLoginUrl(explode(',', FACEBOOK_PERMISSION_SCOPE));?>"> Login </a>
在这里,
命运 - 用户点击登录按钮的页面,这样我就可以在登录过程完成后将其重定向到该页面。
requesthandler.php - 为facebook登录做所有事情的页面。
//requesthandler.php
FacebookSession::setDefaultApplication( FACEBOOK_API_ID, FACEBOOK_API_SECRET );
$helper = new FacebookRedirectLoginHelper( FACEBOOK_REDIRECT_URI_SAME_AS_THE_ONE_IN_REQUEST_PAGE );
try{
$session = $helper->getSessionFromRedirect();
}
catch( FacebookRequestException $ex ){
echo $ex->getResponse();
}
catch (Exception $ex) {
echo $ex->getMessage();
}
$request = new FacebookRequest( $session, 'GET', '/me');
$response = $request->execute();
$userprofile = $response->getGraphObject(GraphUser::classname());
print_r($userprofile->getName());
exit;
返回的错误是:
Catchable fatal error: Argument 1 passed to Facebook\FacebookRequest::__construct() must be an instance of Facebook\FacebookSession, null given, called in /var/www/local/requesthandler.php on line 52 and defined in /var/www/local/classes/apis/facebook/src/Facebook/FacebookRequest.php on line 182