图facebook api用户信息


Graph facebook api user information

如何使用graph API获取用户信息。我正在使用下面的代码,但它不工作,除了图片。

有人能帮我写代码吗?还有关于如何使用facebook php API的初学者的任何书籍或教程,我已经搜索了所有的堆栈溢出问题,教程和书籍,但没有给出清晰的理解。

config。:

<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
use Facebook'FacebookSession;
use Facebook'FacebookRedirectLoginHelper;
use Facebook'FacebookRequest;
use Facebook'FacebookResponse;
use Facebook'FacebookSDKException;
use Facebook'FacebookRequestException;
use Facebook'FacebookAuthorizationException;
use Facebook'GraphObject;
use Facebook'Entities'AccessToken;
use Facebook'HttpClients'FacebookCurlHttpClient;
use Facebook'HttpClients'FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( '1603681436570557','0a18d7df1638ea4ec2a0c2c5c642089c' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://localhost:8888/trender/fbconfig-tr.php' );
try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject();
        $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
        $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
        $femail = $graphObject->getProperty('email');    // To Get Facebook email ID
    /* ---- Session Variables -----*/
        $_SESSION['FBID'] = $fbid;           
        $_SESSION['FULLNAME'] = $fbfullname;
        $_SESSION['EMAIL'] =  $femail;
    /* ---- header location after session ----*/
  header("Location: home-tr.php");
} else {
  $loginUrl = $helper->getLoginUrl();
 header("Location: ".$loginUrl);
}
?>
<<h2> html代码/h2>
<li><?php echo $_SESSION['FULLNAME']; ?></li>
                    <li><img src="https://graph.facebook.com/<?php echo $_SESSION['FBID']; ?>/picture"></li>
                                    <li><img src="https://graph.facebook.com/<?php echo $_SESSION['FBID']; ?>/picture"></li>
                    <li><img src="https://graph.facebook.com/<?php echo $_SESSION['FBID']; ?>/email"></li>
                    <li><img src="https://graph.facebook.com/<?php echo $_SESSION['FBID']; ?>/username"></li>
                                    <li><img src="https://graph.facebook.com/<?php echo $_SESSION['FBID']; ?>/last_name"></li>
                                                    <li><img src="https://graph.facebook.com/<?php echo $_SESSION['FBID']; ?>/birthday"></li>

我已经为facebook制作了样本应用程序,我使用过用户信息,如电子邮件,用户名…我已经在这里托管了代码示例App

    // start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( $app['id'], $app['secret'] );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( $app['url'] );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
  // create new session from saved access_token
  $session = new FacebookSession( $_SESSION['fb_token'] );
  // validate the access_token to make sure it's still valid
  try {
    if ( !$session->validate() ) {
      $session = null;
    }
  } catch ( Exception $e ) {
    // catch any exceptions
    $session = null;
  }
}  
if ( !isset( $session ) || $session === null ) {
  // no session exists
  try {
    $session = $helper->getSessionFromRedirect();
  } catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
    // handle this better in production code
    print_r( $ex );
  } catch( Exception $ex ) {
  // When validation fails or other local issues
    // handle this better in production code
    print_r( $ex );
  }
}
// see if we have a session
if ( !isset( $session ) ) {
  // show login url
  $loginUrl = $helper->getLoginUrl( array( 'publish_actions','email', 'user_photos','user_friends','public_profile','user_about_me','photo_upload','read_friendlists ' ) ) ;// 
  echo '<script>window.top.location="'.$loginUrl.'";</script>';
  exit();
  }
  // save the session
  $_SESSION['fb_token'] = $session->getToken();
  // create a session using saved token or the new one we generated at login
  $session = new FacebookSession( $session->getToken() );

然后我们可以通过这个

得到信息
         //Request Session
         $fbsession = new FacebookRequest( $session, 'GET', '/me' );
         //Execute Session
         $request =   $fbsession->execute();
         $graphArray = $request->getGraphObject()->asArray();
         $username = $graphArray['id'];
         $name     = $graphArray['name'];
         $image    = 'https://graph.facebook.com/'.$username.'/picture?width=219&height=300';
         $gender   =  $graphArray['gender'];