Wednesday 16 November 2011

FaceBook f-connect in cakephp


<span class="facebook">
<a href="<?php echo $this->webroot.'users/facebook_login?first=1';?>">Facebook</a> 
<a href="<?php echo $this->webroot.'users/facebook_login?first=1';?>">
        <img src="<?php echo $this->webroot;?>img/fconnect.png" /></a>
</span>
now code at User controller

/* This function is used to handel fconnect code..when ever a user clicks on fconnect icon at example.com site
the link will redirct it to this function.. where 3 steps will performed
1) if url parameter had 'first' option then redirect to facebook for Authorization code with client_id passed in params
2) It will allow user to enter FaceBook username password and Allow button to click, if cliked it will send Auth Code back,
   redirect url which we collect in 'code' section..then again redirect to FB with Authcode,client_id,secret_key to get
Token_key.
3) on sucessfully getting Token Key again send a request to FB with TokenKey at (c)....
which will return the name,email and other info of that logged in user.
*/
//http://example.com/users/facebook_login
function facebook_login(){
$appId='191645377665635';
$app_secret='b13d9660bcaa0005624f18df4139820';
              //** above API you get after registering the example.com at Facebook to get API key
$cookie=true;
$query="";
if(isset($_SERVER['REQUEST_URI'])){
$str=$_SERVER['REQUEST_URI']; //** get browser URL
$output=parse_url($str);
if(isset($output['query']))
$query=$output['query']; //parameters at url
if(isset($output['first']))
$query=$output['first'];
if(!empty($query)){
$temp=explode("=",$query);
if($temp[0]=="first"){
//*** First Step ************* (a)
$url="http://example.com/users/facebook_login"; 
                                 //callback function should be same in any case,else will show Error in verifcation
$dialog_url="https://www.facebook.com/dialog/oauth?client_id=".$appId."&redirect_uri=".urlencode($url).
"&scope=email,read_stream,offline_access";
echo("<script> top.location.href='".$dialog_url."'</script>");
}
if($temp[0]=="code"){
//** Second Step ************ (b)
$code=$temp[1];
$url="http://example.com/users/facebook_login"; 
                                //callback function should be same in any case,else will show Error in verifcation
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$appId.
"&client_secret=".$app_secret."&code=".$code."&redirect_uri=".urlencode($url);
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token=$params['access_token'];
if(isset($access_token)){
//*** Third Step  ********* (c)
$graph_url = "https://graph.facebook.com/me?access_token=".$access_token;
$user = json_decode(file_get_contents($graph_url));
//print_r($user);
$uname=$user->name; 
$email=$user->email; // ** dipankar@gmail.com - myfacebook mail-id
$gender=$user->gender;
//as we get $uname here now we can write DB quries and other code below.....
}

}
}
}
exit();
}

No comments:

Post a Comment