简体   繁体   中英

Graph facebook api user information

How to get user information using graph API. I'm using the below code but its not working except the picture.

Can someone help me with the code. Also is there any book or tutorial available for beginners on how to use facebook php API, I have searched all stack overflow questions, tutorials and books but nothing gives clear understanding.

config.php :

<?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);
}
?>

html code

<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>

i have maked sample app for facebook where i have used user information like email,username .. i have hosted the code here Sample 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() );

And then we can get information through this

         //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'];   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM