使用Facebook API获取用户数据

时间:2014-06-04 14:05:51

标签: php facebook api

我目前正在记录用户,然后像这样获取/me

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

然后我打印这样的数据,

print_r( $graphObject, 1 )

如果我想获取我要求的所有权限的所有数据,我该怎么办,然后在需要时调用它?

当我将/me更改为/me/tagged_places等时,它适用于该个人权限。我怎么能得到它们?

一旦我拥有它,我想组织它。我可以将这些全部放入一个数组中,然后根据需要调用每个数组吗?如果是这样的话?

这是我的所有代码,

// 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 个答案:

答案 0 :(得分:1)

不确定我是否理解正确,但如果您想查询更多fieldsconnections,请使用所谓的字段扩展:

https://developers.facebook.com/docs/graph-api/using-graph-api/v2.0#fields

例如:

/me?fields=id,first_name,last_name,tagged_places