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
#2

[eluser]maltzurra[/eluser]
One thing is inspiration, another thing copying.

The first part is indeed taken from Haughin's library:

Code:
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');


But you failed to say the second part is also copied from http://github.com/facebook/php-sdk/blob/...xample.php

Code:
//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();
}

I neither want to sound rude nor disrespectful; this code might help people. But in my honest opinion, you simply copy/pasted 2 code snippets on a library.
#3

[eluser]lexusgs430[/eluser]
very cool library thank you for this
#4

[eluser]billmce[/eluser]
Interesting extension of the original work.

Thanks for posting it.
#5

[eluser]The Wizard[/eluser]
@maltzurra

neither part is 1:1 modified. It's structured to the needs of this people here.
If you don't like it, i would strongly suggest you not to use it.

This is a community based on helping each other. In case you didn't notice,
he gave credits.

Stop trolling around.
#6

[eluser]stuffradio[/eluser]
I don't get it, I have the facebook config file and all the required files for logging in... but it keeps saying that the API_Key is undefined when I hit the facebook connect button.

Edit: Nevermind, got it working!
#7

[eluser]nhanco[/eluser]
i'm downloaded do anything you want but i got.
Call to undefined function site_url();
#8

[eluser]Johan André[/eluser]
[quote author="nhanco" date="1292507136"]i'm downloaded do anything you want but i got.
Call to undefined function site_url();[/quote]

that does not have anything to do with this library in particular. you should load the url-helper.




Theme © iAndrew 2016 - Forum software by © MyBB