Skip to content

Instantly share code, notes, and snippets.

Created March 25, 2013 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5237879 to your computer and use it in GitHub Desktop.
Save anonymous/5237879 to your computer and use it in GitHub Desktop.
<?php
/*********************************/
$fbAppId = "150734978329989"; // replace this with your Facebook Apps' App ID
$fbAppSecret = "b62cb0fa9d74e2c8ce74198343938241";
/*********************************/
//Login for serving the Ajax routine
if ( $_GET['ajax']=="yes" ) {
$token = $_POST['token']; //get the access token from the User after he/she authorises the app
$userData = json_decode(file_get_contents("https://graph.facebook.com/me?access_token=".$token), true); //get User's data using the Graph API
echo $fbuid = "Facebook User ID: ".$userData['id']."<br />"; //User's Facebook ID
echo $fbfname = "First Name: ".$userData['first_name']."<br />"; //User's First Name on Facebook
echo $fblname = "Last Name: ".$userData['last_name']; //User's Last Name on Facebook
} else {
require "facebook.php";
$facebook = new Facebook(array(
'appId' => $fbAppId,
'secret' => $fbAppSecret,
'cookie' => true
));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
//Normal HTML Page
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<script src="http://code.jquery.com/jquery-1.6.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function login(varr) {
if ( varr == 1 ) { alert('Please Like This Page First, by Clicking the Like button Above!'); } else {
//this function should be attached to your code's events which will initiate the Facebook login dialog.
//After user authorises this app, we will get the Access Token, which is returned by Facebook in order
//to find the current user' Facebook UID
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
getFBuid(accessToken);
} else if (response.status === 'not_authorized') {
FB.login(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID; //we have got the User's facebook ID here
var accessToken = response.authResponse.accessToken; //Access Token from facebook
getFBuid(accessToken);
} else {
alert("There was an error :(");
}
},{scope:'email'});
} else {
FB.login(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID; //we have got the User's facebook ID here
var accessToken = response.authResponse.accessToken; //Access Token from facebook
getFBuid(accessToken);
} else {
alert("There was an error :(");
}
},{scope:'email'});
}
});
}
}
function getFBuid(token) {
//This function gets User's data from the Graph API with the help of the access token Received
$("#loaderr").show();
$.post("index.php?ajax=yes", {token: token},
function(data) {
$("#loaderr").hide();
alert (data);
$("#fbuid").html(data);
});
}
</script>
</head>
<body>
<div id="fb-root"></div>
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
FB.init({
appId : '<?php echo $fbAppId; ?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
<span id="fbuid">
<?php if ($like_status) { ?>
Facebook User ID: <a href="javascript: login(2)">Click to Get Uid</a> <!-- Note that we have attached the login() function
to the Link as shown here, and explained above-->
<?php } else { ?>
Facebook User ID: <a href="javascript: login(1)">Click to Get Uid</a> <!-- Note that we have attached the login() function
to the Link as shown here, and explained above-->
<?php } ?>
</span>
<br /><br />
<span id="loaderr" style="display: none;">Please Wait...</span>
<br /><br />
Full Tutorial on "How To Get Logged In / Current Facebook User’s ID On Iframe Page Tabs?" here: <a target="_blank" href="http://digitizor.com/?p=10437">digitizor.com/?p=10437</a>
</body>
</html>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment