实际上我使用javascript facebook sdk检索用户配置文件信息。所以我使用以下api调用
FB.api(
"/me",
function (response) {
if (response && !response.error) {
/* handle the result */
}
}
);
所以这个工作很好,我检索了用户详细信息。但是现在我需要使用php检索记录的用户详细信息。我用的是php sdk 4.0
require_once 'Facebook/FacebookSession.php';
require_once 'Facebook/FacebookRedirectLoginHelper.php';
use Facebook'FacebookRequest;
use Facebook'GraphUser;
use Facebook'FacebookRequestException;
use Facebook'FacebookSession;
use Facebook'FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication('****','****');
$helper = new FacebookRedirectLoginHelper( 'http://localhost/' );
$session = $helper->getSessionFromRedirect();
if($session) {
try {
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo "Name: " . $user_profile->getName();
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
但它不工作。这里$session给出空值,请帮助我。
参考API
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/me'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
来源:https://developers.facebook.com/docs/graph-api/reference/v2.0/user
您需要先要求用户登录,然后$session
不会为空。
if ( $session ) {
// your code here
} else {
// show login url
echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}
如果你已经有一个有效的access_token
,那么你可以使用它创建一个新的会话:
$session = new FacebookSession( $access_token );
然后剩下的代码就可以工作了