我无法在 Facebook 中获取用户信息 api php
I cannot get user's informations in facebook api php
我正在为 PHP 使用 facebook api。
我只能获取用户名、全名、link、id 等基本信息。
我无法获取国家、电子邮件、地点等
<?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] => )
您没有要求任何额外的权限,当然您不会在未经所需权限授权的情况下收到电子邮件或其他详细信息。
查看此页面以了解如何在授权过程中请求额外权限:
https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl
例如,如果要获取用户的电子邮件,则必须添加 "email" 权限。对于用户位置,您需要询问 "user_location".
如果需要示例(如何请求权限):
$helper = new FacebookRedirectLoginHelper('http://example.com/callback');
$params = array('email','public_profile', 'user_status', 'user_friends');
$loginUrl = $helper->getLoginUrl($params);
我认为你需要像这样更改 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 );
我正在为 PHP 使用 facebook api。 我只能获取用户名、全名、link、id 等基本信息。 我无法获取国家、电子邮件、地点等
<?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] => )
您没有要求任何额外的权限,当然您不会在未经所需权限授权的情况下收到电子邮件或其他详细信息。
查看此页面以了解如何在授权过程中请求额外权限: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl
例如,如果要获取用户的电子邮件,则必须添加 "email" 权限。对于用户位置,您需要询问 "user_location".
如果需要示例(如何请求权限):
$helper = new FacebookRedirectLoginHelper('http://example.com/callback');
$params = array('email','public_profile', 'user_status', 'user_friends');
$loginUrl = $helper->getLoginUrl($params);
我认为你需要像这样更改 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 );