Ich habe diesen Code kürzlich erstellt, um einem Freund die Facebook-Login-Funktionalität zu erklären. Es gibt ausführliche Kommentare für jede Zeile des Codes. Ich bin mir nicht sicher, ob dies der beste Weg ist, es zu tun. Aber es ist sehr einfach zu verstehen.
<?php
//include Facebook library File
include_once "src/facebook.php";
//Application Configurations
//Declare variables app_id,app_secret, we get app_id,app_secret values from app dashboard.
$app_id = "xxxxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Create our application instance
//we create $facebook instance and would use it to access various Facebook library functions
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
?>
<html>
<head>
</head>
<body>
<?php
// Get User ID
$user = $facebook->getUser();
// $facebook->getUser() is a function which would return a user id if someone is
// logged in on Facebook currently (In some other window or tab)
// We may or may not have this data based
// on whether the user is logged in.
// If we have a $user id here, it means we know
// the user is logged into Facebook,
// but we don’t know if the user has authorized our application.
// i.e whether user has already given our app permissions to access his data or not
if ($user) {
/*
* Facebook user retrieved
* $user : Holds the Facebook Users unique ID
* */
try {
// We try to get users data assuming he has authoized are app if we dont get data we display login button in catch block.
$userinfo = $facebook->api('/me');
echo "<br/>User id : ".$userinfo['id'];
echo "<br/>Name : ".$userinfo['name'];
echo "<br/>Gender : ".$userinfo['gender'];
echo "<br/>Email : ".$userinfo['email'];
echo "<br/>Location : ".$userinfo['location']['name'];
echo "<br/>Image : <img src='https://graph.facebook.com/".$userinfo['id']."/picture' >";
echo "<br/><br/><br/>Similarly we can get lot more information about user like work experience, dob etc more details would be shared later /We use this data to prefill registration form for user. ";
echo "<br/><br/>Below is dump of entire array which shows all available data it carries<br/><br/>";
echo '<pre>';
var_dump($userinfo);
echo '</pre>';
//Similarly we can get lot more information about user like work experience, dob etc more details would be shared later
//We use this data to prefill registration form for user.
} catch (FacebookApiException $e) {
// Display Facebook login button - Requires Facebook JavaScript SDK (Below)
echo '<fb:login-button show-faces="true" width="200" scope="email,user_photos"></fb:login-button>'."\n";
$user = NULL;
}
} else {
// No user logged in currently Display Facebook login button - Requires Facebook JavaScript SDK (Below)
echo '<fb:login-button show-faces="false" width="200" scope="email,user_photos"></fb:login-button>'."\n";
}
?>
<!--Below code is required to use FAcebook Javascript SDK/Library, Before this we have been using Facbeook PHP-SDK/Library -->
<div id="fb-root"></div> <!-- A div element with id "fb-root" is required. Facebook JS SDK will auto create this element if it doesn't exist , But we will anyways create it-->
<script>
// window.fbAsyncInit will execute code within it asynchronously i.e without affecting the load time.
window.fbAsyncInit = function () {
//Below code Initializes Javacript SDK, appid is same as one defined in fbaccess.php
FB.init({
appId : '<?=$app_id?>',
cookie : true,
xfbml : true,
oauth : true
});
//Below code tells the script to refresh whenever user logins or logouts.
FB.Event.subscribe('auth.login', function (response) { window.location.reload(); });
FB.Event.subscribe('auth.logout', function (response) { window.location.reload(); });
};
// Copy paste code to load the Facebook JavaScript SDK into the page
(function(){var e=document.createElement('script');e.async=true;e.src=document.location.protocol+'//connect.facebook.net/en_US/all.js';document.getElementById('fb-root').appendChild(e);}());
</script>
</body>
</html>
In Javascript können Sie eine Funktion wie diese aufrufen:
function login(){
FB.getLoginStatus(function(response) {
if (response.authResponse) {
alert(response.authResponse.userID);
//Authenticated user.
} else {
// Unauthenticated user display authentication popup
FB.login(function(response) {
if (response.authResponse) {
alert(response.authResponse.userID);
//User authenticated.
}
else{
alert("Please allow authentication to proceed!!");
}
}, {scope:'email'});
}
});
}