Facebook Connect (Graph API) in cake php

Posted by deepak on in Cakephp

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>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WP Hashcash

bottombar