Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Login: How to combine JavaScript with PHP SDK?

Tags:

How do I go about combining examples from both

https://github.com/facebook/php-sdk/blob/master/examples/example.php and https://github.com/facebook/connect-js/blob/master/examples/jquery/login.html

The end goal is so that we can use jQuery to sign in, and somehow the following line would still work

$user = $facebook->getUser(); 

What I currently notice is that when I sign in with JavaScript, it doesn't set the session with PHP, hence, it is incompatible with PHP based SDK (so $user in this case would still be null)

I am not a big fan of <a href="$facebook->getLoginUrl()">Login with Facebook</a>, because it causes the browser to load a different page (the facebook login) and then redirects back to the web app. It is far more ideal if I can load up a popup box for user to signin, and then transparently redirects back to my web app. So any suggestions on how I can go about implementing this user experience would be greatly appreciated. Thank you!

like image 372
Antony Avatar asked May 27 '11 03:05

Antony


People also ask

What is Facebook SDK for JavaScript?

The Facebook SDK for JavaScript provides a rich set of client-side functionality that: Enables you to use the Like Button and other Social Plugins on your site. Enables you to use Facebook Login to lower the barrier for people to sign up on your site. Makes it easy to call into Facebook's Graph API.


2 Answers

Not sure if you got this working. The current JS API has a configuration to save the cookie so the server may read it:

FB.init({     appId  : 'YOUR APP ID',     cookie : true }); 

Now, in PHP, you can do the following to use the session from the JS:

// Read the cookie created by the JS API $cookie = preg_replace("/^\"|\"$/i", "", $_COOKIE['fbs_' . FB_APP_ID]); parse_str($cookie, $data);  // Startup the Facebook object $fb = new Facebook(array(     'appId'  => FB_APP_ID,     'secret' => FB_APP_SECRET ));  // Say we are using the token from the JS $fb->setAccessToken($data['access_token']);  // It should work now var_dump($fb->getUser()); 

Hope it helped.

like image 92
Pedro Villa Verde Avatar answered Sep 20 '22 13:09

Pedro Villa Verde


I just use the Facebook login button - https://developers.facebook.com/docs/reference/plugins/login/

Once the user has used that to login (if they weren't already) and grant access to your app (site), the PHP-SDK should have no trouble using the Facebook session and APIs.

Update

I just tried this with the new version 3.0 PHP SDK and it is definitely not working. Found this on the developer blog

If you are using the JavaScript SDK for login in conjunction with the PHP SDK, you will want to wait for the JavaScript SDK upgrade (coming in 4 weeks). Version 3.0.0 of the PHP SDK won’t cooperate with the current JavaScript SDK due to the cookie format changing.

In other words, use the v2 PHP SDK until the JS SDK is updated.

like image 42
Phil Avatar answered Sep 21 '22 13:09

Phil