dongshan4549
2014-06-04 14:05 阅读 149
已采纳

使用Facebook API获取用户数据

I am currently logging a user in and then getting /me like this,

$graphObject = (new FacebookRequest( $session, 'GET', '/me/tagged_places' ))->execute()->getGraphObject()->asArray();

Then I am printing the data like this,

print_r( $graphObject, 1 )

If I want to get all the data for all the permissions I am asking for how can I do so then call on it when I need it?

When I change /me to /me/tagged_places etc. it works for that individual permission. How can I get them all?

Also once I have it I would like to organize it. Can I put this all into an array and then call each one as needed? If so how?

Here is all of my code,

// include required files form Facebook SDK

// added in v4.0.5
require_once( 'facebook/src/Facebook/FacebookHttpable.php' );
require_once( 'facebook/src/Facebook/FacebookCurl.php' );
require_once( 'facebook/src/Facebook/FacebookCurlHttpClient.php' );
require_once( 'facebook/src/Facebook/FacebookSession.php' );
require_once( 'facebook/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'facebook/src/Facebook/FacebookRequest.php' );
require_once( 'facebook/src/Facebook/FacebookResponse.php' );
require_once( 'facebook/src/Facebook/FacebookSDKException.php' );
require_once( 'facebook/src/Facebook/FacebookRequestException.php' );
require_once( 'facebook/src/Facebook/FacebookOtherException.php' );
require_once( 'facebook/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'facebook/src/Facebook/GraphObject.php' );
require_once( 'facebook/src/Facebook/GraphSessionInfo.php' );

use Facebook\FacebookHttpable;
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;

// start session
session_start();

// init app with app id and secret
FacebookSession::setDefaultApplication( 'foo','foo' );

 // login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://www.website.com/' );


// 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;
  }

} else {
  // 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 ) ) {

  // 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() );

  // graph api request for user data with response
$graphObject = (new FacebookRequest( $session, 'GET', '/me/tagged_places' ))->execute()->getGraphObject()->asArray();
  }
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐