Facebook登录:如何将JavaScript与PHP SDK结合起来?

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!

dthl8036 使用最新的api,您无需做任何事情。只要确保cookie已开启,那么phpSDK将起作用。
7 年多之前 回复
drq1257 看到我更新的答案
9 年多之前 回复
duanji1043 我有同样的问题。你明白了吗?
9 年多之前 回复


我只使用Facebook登录按钮 - https://developers.facebook.com/docs/reference/plugins/login/ </ p>

一旦用户使用它登录(如果他们不是 (已经)并授予对您的应用(网站)的访问权限,PHP-SDK应该可以毫无困难地使用Facebook会话和API。</ p>

更新</ h2>

< 我只是尝试使用新版本3.0 PHP SDK,它绝对是不</ strong>工作。 在开发人员博客上找到了这个</ p>

如果你 我们正在使用JavaScript SDK与PHP SDK一起登录,您将需要等待JavaScript SDK升级(即将在4周内完成)。 由于cookie格式的改变,PHP SDK 3.0.0版本不会与当前的JavaScript SDK合作。</ p>
</ blockquote>

换句话说,使用v2 PHP SDK 直到JS SDK更新。</ p>
</ div>



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.


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.

duannong1801 这很有趣,我作为Sandbox'ed App Test用户登录到facebook。 这导致博客页面抛出500 ..
接近 8 年之前 回复
dongsaolian8786 你确定? 打开对我来说很好
接近 8 年之前 回复
douqiong8412 这个与开发博客的链接似乎不再起作用了。 有更新的吗?
接近 8 年之前 回复
douwen5924 谢谢菲尔! 我降级到v2,现在它正在工作! 在旁注中,如果由于针对localhost进行测试而导致v2运行进入证书问题的任何人,请参阅github.com/facebook/php-sdk/issues/79以获取解决方案。
9 年多之前 回复
duankuangxie9070 问题是所有示例都可以让您克隆/下载主分支。 他们应该真正将v3分支标记为beta,直到JS SDK赶上来
9 年多之前 回复
doushiposong30622 谢谢菲尔。 我回到此页面评论我刚刚在博客上阅读了该片段。 在过去的八个小时里,我一直在试着把头发弄出来! 那好吧...
9 年多之前 回复
dongwen9947 你是完全正确的。 似乎PHP SDK不再能够通过JavaScript SDK访问cookie集。 最重要的是,我无法使用getLogoutUrl()方法注销
9 年多之前 回复
duanniu3385 实际上,昨天我花了很长时间试图让你的建议工作,但这是不行的。 我修改了PHP SDK中的示例,如pastebin.com/fy2SRLWM所示。即使我已登录(fb:login-button会显示我已登录),它也不会设置PHP会话。 在我的应用程序设置中,siteUrl是localhost,用于本地测试
9 年多之前 回复

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

    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

// It should work now

Hope it helped.

douyihuaimao733955 除此之外,cookie上的域必须是正确的 - 对我来说,fb app设置> App域需要使用裸域名:example.com而不是www.example.com。 这可能仅适用于我有多个子域名(www,sales.example.com等)的情况。
5 年多之前 回复
dongshi2836 你甚至不需要做所有preg_replace的东西了。 getUser()将在盒子外面工作
6 年多之前 回复
dongshi3605 它对我有用,但我不得不使用$ _COOKIE ['fbm_'。 FB_APP_ID]非常感谢。
7 年多之前 回复

听起来像facebook没有在正确的时间解决这个问题(因为他们谈论 https://developers.facebook.com/blog/post/503 。</ p>

这个 是关于js sdk
的最新信息 https://developers.facebook.com/blog/post/ 525 / </ p>
</ div>



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 sdk https://developers.facebook.com/blog/post/525/

After long hours of searching, I found this fixed my problem where the PHP SDK did not pick up the Facebook session. Apparently, the Javascript SDK is not setting the session the right way. Add this to your auth.login eventlistener:

FB.Event.subscribe('auth.login', function(response) {
    FB._oauth = false;
    FB.Auth.setSession(response.authResponse, response.status);
    FB._oauth = true;

Also, make sure you set the App Domain to match your cookie domain. After fixing these items, it finally worked.

Edit: This morning, everything was working fine. When I was testing some more just now, I found that I had to edit base_facebook.php to make sure the requests to Facebook would not time-out. My guess is that because I am testing on my local machine, the connection would not be fast enough to make it in time. This explains the strange behaviour where sometimes it would and sometimes it would not work.

Alter the connecttimeout in the $CURL_OPTS array on line 128:

public static $CURL_OPTS = array(
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_USERAGENT      => 'facebook-php-3.1',

In version 4 of the SDK it works automatically with the JavaScript SDK. You just need to use FacebookJavaScriptLoginHelper:

$helper = new FacebookJavaScriptLoginHelper(); // Get access token from JS SDK
try {
    $session = $helper->getSession();
} catch(FacebookRequestException $e) {
    // When Facebook returns an error
    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();
} catch(\Exception $e) {
    // When validation fails or other local issues
    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();

// If logged in
if($session) {
    // Get user profile
    try {
        $user_profile = (new FacebookRequest(
            $session, 'GET', '/me'


    } catch(FacebookRequestException $e) {

        echo "Exception occured, code: " . $e->getCode();
        echo " with message: " . $e->getMessage();

No need to mess around with cookies!

Csdn user default icon