使用php检索facebook用户信息

使用php检索facebook用户信息,php,facebook-graph-api,facebook-php-sdk,Php,Facebook Graph Api,Facebook Php Sdk,实际上,我使用JavaScriptFacebookSDK来检索用户配置文件信息。所以我使用了下面的api调用 FB.api( "/me", function (response) { if (response && !response.error) { /* handle the result */ } } ); 因此,这项工作很好,我检索到了用户详细信息。但现在我需要使用php检索记录的用户详细信

实际上,我使用JavaScriptFacebookSDK来检索用户配置文件信息。所以我使用了下面的api调用

FB.api(
    "/me",
    function (response) {
        if (response && !response.error) {
            /* handle the result */
        }
    }
);
因此,这项工作很好,我检索到了用户详细信息。但现在我需要使用php检索记录的用户详细信息。我使用了
PHPSDK4.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 */

来源:

您需要先要求用户登录,然后
$session
将不会为空

if ( $session ) {

  // your code here

} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}

然后剩下的代码就可以工作了。

请告诉我如何创建$session对象
$session = new FacebookSession( $access_token );