12

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. :)

1

5 Answers 5

2

Try this Facebook Library by Danny Tran hosted on Github. Works wonders and is fairly easy to get set-up.

2

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

2

These links might solve your any facebook programming problems.

http://developers.facebook.com/docs/authentication/

http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

http://realfoot.com/facebook_connect/login_page.php

Facebook Login Button: Can I prevent auto page reload after login?

Codeignitor, Facebook javascript SDK, PHP SDK Redirect after facebook login doesnt getUser() until refresh

http://www.braintilt.com/fbcookies.php

http://www.permadi.com/blog/2011/02/facebook-authenticationlogin-process-in-popup-or-not/

http://developers.facebook.com/docs/reference/javascript/FB.login/

http://ottopress.com/wordpress-plugins/simple-facebook-connect/

http://devtacular.com/articles/bkonrad/how-to-integrate-with-facebook-connect/

http://developers.facebook.com/docs/guides/web/

http://www.1stwebdesigner.com/tutorials/facebook-integrate/

http://worldpointx.com/2009/05/authentication-rot/

https://github.com/facebook/php-sdk/

http://www.justin-klein.com/projects/wp-fb-autoconnect

http://en.kioskea.net/forum/affich-12130-problems-connecting-to-facebook

http://en.kioskea.net/s/Cannot+login+on+website+using+facebook+login

http://www.google.com/support/forum/p/Chrome/thread?tid=310bc994b12d2750&hl=en http://www.google.com/support/forum/p/Chrome/thread?tid=310bc994b12d2750&hl=en

http://moodle.org/mod/forum/discuss.php?d=142036

http://www.webtechnick.com/blogs/view/229/CakePHP_Facebook_Plugin_Auth_Facebook_and_more

0
1

Did you add xd_reciever file to your public directory????

Probabiliy this may causing the problem bcoz xd_reciever deifine the channel between your app to facebook.

1

This post may be useful for you. Check it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.