PHP SCRIPT FOR LOGIN WITH FACEBOOK USING OAUTH LIBRARY

PHP SCRIPT FOR LOGIN WITH FACEBOOK USING OAUTH LIBRARY

In this Tutorial we are going to prepare a php script for login and authentication of a user on particular website via Facebook PHP Oauth Library. Facebook has provided capability to create an app with Api Key and Api Secret Which are always Unique for every user Application created by a particular. So you just need to go to FACEBOOK APPS and Create a New App and Publish it live and Note Down Your API Key and API Secret . Okay Now lets Get Down to Business. First we will need the Facebook Official php library. So You can Download it from Facebook official Site or from this LINK Now We need to build our own Facebook class to setup the functions which will connect us with Facebook API So , Here is the Code ::::

<?php
require 'facebook.php';
class facebook_login
{
function facebook_login($id,$secret,$callback="",$permission=0)
{
if(!$permission)
{
$permission = "email,user_birthday,user_location";
}
else
{
$permission = $permission;
}
if($permission==-1)
{
$permission = "";
}
$this->ApplicationID = "$id";
$this->ApplicationSecret = "$secret";
$this->CallBack = "$callback";
$this->Permission = "$permission";
$this->facebook = new Facebook(array(
'appId' => $this->ApplicationID,
'secret' => $this->ApplicationSecret,
'cookie' => true,
));
}
function connection()
{

$session = $this->facebook->getSession();
$me = null;
if($session)
{
try
{
$uid = $this->facebook->getUser();
$me = $this->facebook->api('/me');
}
catch(FacebookApiException $e)
{
error_log($e);
}
}


return "<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function()
{
FB.init
({
appId : '".$this->facebook->getAppId()."',
session : ".json_encode($session).",
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.login', function()
{
window.location.reload();
});
};

(function()
{
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>

<fb:login-button perms="".$this->Permission."" onlogin='window.location="".$this->CallBack."";'>Connect</fb:login-button>
</body>
</html>";
}
function InformationInfo()
{
if($_REQUEST["fbs_".$this->ApplicationID]=="")
return "Permission Disallow!";
$PermissionCheck = split(",",$this->Permission);
$a = str_ireplace(""","",$_REQUEST["fbs_".$this->ApplicationID]);
if(!$a)
{
return "Permission Disallow!";
}
$user = json_decode(file_get_contents('https://graph.facebook.com/me?'.$a));
$Result["UserID"] = $user->id;
$Result["Name"] = $user->name;
$Result["FirstName"] = $user->first_name;
$Result["LastName"] = $user->last_name;
$Result["ProfileLink"] = $user->link;
$Result["ImageLink"] = "<img src='https://graph.facebook.com/".$user->id."/picture' />";
$Result["About"] = $user->about;
$Result["Quotes"] = $user->quotes;
$Result["Gender"] = $user->gender;
$Result["TimeZone"] = $user->timezone;
if(in_array("email",$PermissionCheck))
{
$Result["Email"] = $user->email;
}
if(in_array("user_birthday",$PermissionCheck))
{
$Result["Birthday"] = $user->birthday;
}
if(in_array("user_location",$PermissionCheck))
{
$Result["PermanentAddress"] = $user->location->name;
$Result["CurrentAddress"] = $user->hometown->name;
}
return $Result;
}
function FBLogin()
{
$session = $this->facebook->getSession();
$me = null;
if($session)
{
try
{
$uid = $this->facebook->getUser();
$me = $this->facebook->api('/me');
}
catch(FacebookApiException $e)
{
error_log($e);
}
}
if($me)
{
return $this->InformationInfo();
//return "<a href=".$this->facebook->getLogoutUrl()."><img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif"></a>";
}
else
{
return $this->connection();
}
}
}
?>

Ok , So Above class is very Important because it sets the scope of our Application that what information we can access of a particular user : Permissions :

  1. email : To Access the email address of the user
  2. user_friends : to access the friend list of user
  3. app_friends : to access friends of user which are commonly using your app
  4. user_location : to access location of user
  5. read_stream : to read the posts of user
  6. friends_online_presence : which friends of user are online on facebook
  7. publish_stream : publish post on user’s wall on behalf of them

So , for starting you should only take permission to read email and public profile which facebook by default provides when you publish your App. For more permissions you need to submit your application to facebook with privacy policy and Terms of condition and your website URL . Then Facebook will review your website and approve or disapprove your request Accordingly. Alright , Now the main file which you need to run on browser. Create a file with name test.php with following code :

<?php
/*
* Your facebook convas url and your facebook connect url must be same.
* I-E your facebook convas url is http://www.example.com/a/abc.php
* and your facebook connect url also same as http://www.example.com/a/.*
*/
include "facebook.class.php";
/* Create our Application instance (replace this with your appId and secret).
*/
$id = "YOUR APP ID HERE"; /* facebook applicationh ID. */
$sec = "YOUR APP SECRET HERE"; /* facebook application secrate. */
$callback = "http://www.example.com/abc.php"; /* call back url ie http://www.example.com/abc.php/ */
$permission = "0"; /* 0 for all class custome permission or explain ur permission like offline_status, email,sms etc http://developers.facebook.com/docs/authentication/permissions. */
$facebook = new facebook_login($id,$sec,$callback,$permission);

if(is_array($facebook->FBLogin()))
{
print_r($facebook->FBLogin());
}
else
{
echo $facebook->FBLogin();
}
?>

That’s All You Got all of your Information. DEMO LINK : LOGIN WITH FACEBOOK

6 thoughts on “PHP SCRIPT FOR LOGIN WITH FACEBOOK USING OAUTH LIBRARY

Leave a comment