如何使用graph
API获取用户信息。我使用下面的代码,但除了图片之外它没有用。
有人可以帮我解决这些问题。还有任何关于如何使用facebook php API的初学者的书或教程,我已经搜索了所有堆栈溢出问题,教程和书籍,但没有任何内容给出清晰的理解。
的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);
}
?>
<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>
答案 0 :(得分:-3)
我已经为facebook制作了示例应用程序,其中我使用了电子邮件,用户名等用户信息..我在此处托管了代码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() );
然后我们可以通过此
获取信息 //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'];