I'm having problems trying to connect my website to Facebook. What I would like it to do is allow users to augment their accounts by connecting them to Facebook (I do not need people to be able to login with Facebook, I am using OpenID instead). My website runs in the CodeIgniter framework.
What I will do is remove everything that I've tried and walk you through my steps up until it stops working.
Step 1: Set up the JavaScript SDK and XFBML
First I add a reference to the FBML spec to my <html>
tag:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en" lang="en">
Then, after my <body>
tag, I add:
<script src="http://static.new.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
Now here comes the first problem I had but have now solved. The problem was that the docs for the JavaScript SDK say that the FB.init()
method requires the appId as a parameter, but this turned out to be incorrect. I needed to pass my apiKey instead. So this is the code that I ended up with, that goes before my </body>
tag:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
apiKey : '<?php echo $this->config->item('facebook_api_key'); ?>',
status : true,
cookie : true,
xfbml : true
});
</script>
Now I can add XFBML entities to my pages, such as <fb:login-button></fb:login-button>
which works just as expected. I click it and it pops up a box asking if I want to allow my website to access my Facebook profile. I say yes, and then if I check my Application Settings in Facebook, I can see that my website has been added.
So this step works fine.
Step 2: Set up the PHP library
Okay, so now I need to add the PHP library. So a quick google search for 'codeigniter facebook library' reveals Elliot Haughin's library. I download the files, put them all in the right places then get to work on loading the library.
Now the current problem occurs. I add the following code (taken from the example given with the library) to my controller constructor so that it happens on every page:
$this->load->library('facebook_connect');
$this->data['fb_user'] = $this->facebook_connect->user;
$this->data['fb_userid'] = $this->facebook_connect->user_id;
But if I do print_r($this->data['fb_user']);
I get nothing. Ever. Whether I'm logged into facebook, logged out, logged in and added my site to my profile, etc. etc. etc. The user and user_id are never set. I don't get any errors though.
As far as I'm aware, the values are supposed to be set so that I know whether the person browsing my site is logged into their facebook account. If they are, I can see if they've allowed my application. If they've allowed it, I can let them disconnect from my site, if not, I can let them connect. But I can't do anything if these variables aren't populated.
I've tried poking around inside the source of the library, but I really can't get my head around it. And I'm hoping that the actual solution for this won't involve hacking the library apart. I'd expect a CodeIgniter specific library to work.
Any ideas?
Thanks a lot. :)
Try this Facebook Library by Danny Tran hosted on Github. Works wonders and is fairly easy to get set-up.
You are loading the new (http://connect.facebook.net/en_US/all.js) and the old (http://static.new.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php) JS SDKs. Remove the reference to the older one. The new JS SDK will set a signed cookie, which can be read (securely) using the new PHP SDK: http://github.com/facebook/php-sdk. This auth example for the JS SDK might help too: http://fbrell.com/auth/all-in-one
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With