我无法在facebook api php中获取用户的信息

时间:2015-02-16 07:04:49

标签: php facebook facebook-graph-api facebook-javascript-sdk facebook-php-sdk

我正在使用facebook api for PHP。 我只能获得用户名,全名,链接,身份证等基本信息。 我无法获得国家,电子邮件,地方..等等。

<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
require_once( 'src/Facebook/FacebookSession.php');
require_once( 'src/Facebook/FacebookRequest.php' );
require_once( 'src/Facebook/FacebookResponse.php' );
require_once( 'src/Facebook/FacebookSDKException.php' );
require_once( 'src/Facebook/FacebookRequestException.php' );
require_once( 'src/Facebook/FacebookRedirectLoginHelper.php');
require_once( 'src/Facebook/FacebookAuthorizationException.php' );
require_once( 'src/Facebook/GraphObject.php' );
require_once( 'src/Facebook/GraphLocation.php' );
require_once( 'src/Facebook/GraphUser.php' );
require_once( 'src/Facebook/GraphSessionInfo.php' );
require_once( 'src/Facebook/Entities/AccessToken.php');
require_once( 'src/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'src/Facebook/HttpClients/FacebookHttpable.php');
require_once( 'src/Facebook/HttpClients/FacebookCurlHttpClient.php');

/* USE NAMESPACES */

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\GraphLocation;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookCurl;
// init app with app id and secret
FacebookSession::setDefaultApplication( '518573344952566','a3afc2397a81e216e60714f83142e2f9' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://localhost/1353/fbconfig.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'); 
$fplace=$graphObject->getProperty('place'); 
    /* ---- Session Variables -----*/

// Get the base class GraphObject from the response
// Get the base class GraphObject from the response
$object = $response->getGraphObject();

// Get the response typed as a GraphUser
$user = $response->getGraphObject(GraphUser::className());
// or convert the base object previously accessed
// $user = $object->cast(GraphUser::className());

// Get the response typed as a GraphLocation
$loc = $response->getGraphObject(GraphLocation::className());
// or convert the base object previously accessed
// $loc = $object->cast(GraphLocation::className());

// User example
echo $object->getProperty('name');
echo $user->getName();


// Location example
echo $object->getProperty('country');
echo $loc->getCountry();
 echo $object->getProperty('email');
// SessionInfo example
$info = $session->getSessionInfo();
echo $info->getxpiresAt();
    /* ---- header location after session ----*/
  header("Location: index.php");
} else {
  $loginUrl = $helper->getLoginUrl();
 header("Location: ".$loginUrl);
}
?>

这是我的代码,当我像国家,电子邮件,地方一样打印时,它什么也没显示! 当我打印$response时 它显示以下

Facebook\FacebookRequest Object ( [session:Facebook\FacebookRequest:private] => Facebook\FacebookSession Object ( [accessToken:Facebook\FacebookSession:private] => Facebook\Entities\AccessToken Object ( [accessToken:protected] => CAAHXo8QEEPYBAPwxcD0DT08AuysfOCGl7TWYCViyZB0PnXFQGB5LrfreJh3hxM1xtDQpD7zqJReTJESo96VS4Wi2qIQRKzpMaWZCBkTFIlHzyhbq96k8aXJGdS77jysotckuGEcgyCPJp7veRyJL4SZCN8a9m9sZAKtcvS3ZCLO6COorMTQKSYAyJAXkYuIb1biS7sLd0MMhH3c9aylAJ [machineId:protected] => [expiresAt:protected] => ) [signedRequest:Facebook\FacebookSession:private] => ) [method:Facebook\FacebookRequest:private] => GET [path:Facebook\FacebookRequest:private] => /me [params:Facebook\FacebookRequest:private] => Array ( [access_token] => CAAHXo8QEEPYBAPwxcD0DT08AuysfOCGl7TWYCViyZB0PnXFQGB5LrfreJh3hxM1xtDQpD7zqJReTJESo96VS4Wi2qIQRKzpMaWZCBkTFIlHzyhbq96k8aXJGdS77jysotckuGEcgyCPJp7veRyJL4SZCN8a9m9sZAKtcvS3ZCLO6COorMTQKSYAyJAXkYuIb1biS7sLd0MMhH3c9aylAJ [appsecret_proof] => fbd9e3248da5678fc68be8bfcee6368ed56fdf71507d16d7b19dc494c22280dc ) [version:Facebook\FacebookRequest:private] => v2.2 [etag:Facebook\FacebookRequest:private] => ) 

3 个答案:

答案 0 :(得分:1)

您没有要求任何其他权限,当然,如果没有使用所需权限进行授权,您将无法收到电子邮件或其他详细信息。

查看此页面,了解如何在授权过程中要求其他权限: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl

例如,如果您想获取用户的电子邮件,则必须添加“email”权限。对于用户位置,您需要询问“user_location”。

答案 1 :(得分:1)

如果需要示例(如何询问权限):

$helper = new FacebookRedirectLoginHelper('http://example.com/callback');
    $params = array('email','public_profile', 'user_status', 'user_friends');
    $loginUrl = $helper->getLoginUrl($params);

答案 2 :(得分:1)

我认为您需要更改这样的API请求,然后您肯定会获得Facebook电子邮件ID。

$request = new FacebookRequest( $session, 'GET', '/me?fields=email' );

如果您想获得更多用户信息,可以这样:

$request = new FacebookRequest( $session, 'GET', '/me?fields=email,first_name,last_name,gender'.......etc );