Facebook Connect (Graph API) in cake php

Facebook  Connect (Graph API)  in cake php 08Dec, 2011

To implement Facebook connect using graph API you need to follow the steps

1) Extract the facebook_core_files.zip file and put all of them in vendors folder of cake php

2) In your apps controller include this code at the top of the file App::import(‘Vendor’, ‘facebook’, array(‘file’ => ‘facebook.php’));

3) in your apps controller add this function ( before filter )

function beforeFilter()
{
//facebook application
$user = null; //facebook user uid
$userInfo = null;

// Create our Application instance.
$facebook = new Facebook(array(
‘appId’ => YOURAPPID,
‘secret’ => YOURSECRET
));

//Facebook Authentication part
$user = $facebook->getUser();

if ($user) {
try {
// Proceed knowing you have a logged in user who’s authenticated.
$fql = “select name,current_location,email,sex,verified,pic_square from user where uid=” . $user;
$param = array(
‘method’ => ‘fql.query’,
‘query’ => $fql,
‘callback’ => ”
);

$userInfo = $facebook->api($param);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
}

$userInfo variable will contain list of data returned by facebook .

In your BODY of HTML page put the below code

<div id=”fb-root”></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: ‘APPID’,
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe(‘auth.login’, function(response) {
window.location.href = ‘/users/facebookLogged’;
});
FB.Event.subscribe(‘auth.logout’, function(response) {
window.location.reload();
});
};
(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>
<fb:login-button autologoutlink=”true” scope=”email,user_birthday,status_update,publish_stream,user_location,offline_access”></fb:login-button>

Posted by: Deepak Saini / In: Cakephp
Cam