Facebook Login: How to combine JavaScript with PHP SDK? Facebook Login: How to combine JavaScript with PHP SDK? jquery jquery

Facebook Login: How to combine JavaScript with PHP SDK?


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 nowvar_dump($fb->getUser());

Hope it helped.


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.


it's sound like facebook didn't fix this problem at the right time(as they talk https://developers.facebook.com/blog/post/503).

this is newest infomation about js sdkhttps://developers.facebook.com/blog/post/525/