Sunday, March 13, 2011

Merging Facebook SDK with CodeIgniter 2.0 v1.1

After talking with folks at codeigniter.com and a bit of peer review I have come to the conclusion that the library doesn't need to have variables declared every time. In fact you can create a file named facebook.php in the /application/config folder and throw in:
$config['appId'] = 'your_app_id';
$config['secret'] = 'your_app_secret';
$config['cookie'] = true;
After which, go to /application/config/autoload.php and add 'facebook' to the libraries array and it will automatically load those parameters into your library.
Afterwards your calling of the functions should look like this in the __construct() function:
function __construct()
{
parent::__construct();

$this->fb_session = $this->facebook->getSession();
$this->fb_me = null;
// Session based API call.
if ($this->fb_session) {
try {
$this->fb_uid = $this->facebook->getUser();
$this->fb_me = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
$this->fb_appid = $this->facebook->getAppId();

$this->data['appid'] = $this->fb_appid;
$this->data['me'] = $this->fb_me;
$this->data['uid'] = $this->fb_uid;
}
Hope this helps you with what you were looking for! Stay tuned for more as I discover more interesting things about the facebook sdk and codeigniter. Next time I will include a tutorial on how to make sure that your users get authenticated correctly via the system.

No comments:

Post a Comment