Welcome Guest, Not a member yet? Register   Sign In
Facebook Connect (New Graph API) library and login integration tutorial
#1

[eluser]Unknown[/eluser]
Hey Everyone!

We've been using CI for a while now and figured it was time to give something back. One of the biggest problems we faced was integrating Facebook connect with our login system. Inspired by Haughin(Shout Out), we decided to help others integrate facebook with their CI apps. ;-)

Check out the link to see the tutorial, download the code, and leave comments etc.(you can post here too if you'd like)
Facebook Connect User Authentication using the new Graph API in CodeIgniter
download the zip
follow us on twitter too

I realize some of you will want to look over the code... so here's the majority of it:
Code:
<?php
//fb_connect.php
//Author: Graham McCarthy,  HitSend Inc., September 29th, 2010
//Email: [email protected]
//Description: facebook connect library, connects to facebook and
//    stores all the required information in return variables
//grab the facebook api php file
include(APPPATH.'libraries/facebook/facebook.php');

class Fb_connect {
//declare variables
private $_obj;
private $_api_key        = NULL;
private $_secret_key    = NULL;

public     $user             = NULL;
public     $user_id         = FALSE;

public $fbLoginURL     = "";
public $fbLogoutURL = "";

public $fb             = FALSE;
public $fbSession    = FALSE;
public $appkey        = 0;

//constructor method.
function Fb_connect()
{
//Using the CodeIgniter object, rather than creating a copy of it
$this->_obj =& get_instance();

//loading the config paramters for facebook (where we stored our Facebook API and SECRET keys
$this->_obj->load->config('facebook');
//make sure the session library is initiated. may have already done this in another method.
$this->_obj->load->library('session');

//
$this->_api_key        = $this->_obj->config->item('facebook_api_key');
$this->_secret_key    = $this->_obj->config->item('facebook_secret_key');

$this->appkey = $this->_api_key;

//connect to facebook
$this->fb = new Facebook(array(
'appId'  => $this->_api_key,
'secret' => $this->_secret_key,
'cookie' => true,
));

//store the return session from facebook
$this->fbSession  = $this->fb->getSession();

$me = null;
// If a valid fbSession is returned, try to get the user information contained within.
if ($this->fbSession) {
try {
//get information from the fb object
$uid = $this->fb->getUser();
$me = $this->fb->api('/me');

$this->user = $me;
$this->user_id = $uid;

} catch (FacebookApiException $e) {
error_log($e);
}
}

// login or logout url will be needed depending on current user state.
//(if using the javascript api as well, you may not need these.)
if ($me) {
$this->fbLogoutURL = $this->fb->getLogoutUrl();
} else {
$this->fbLoginURL = $this->fb->getLoginUrl();
}
} //end Fb_connect() function
} // end class

The tutorial and DB schema are here: tutorial and examples
If you want a direct link to the download click here: download the zip


Messages In This Thread
Facebook Connect (New Graph API) library and login integration tutorial - by El Forum - 10-06-2010, 11:19 PM



Theme © iAndrew 2016 - Forum software by © MyBB