Facebook 登录:如何结合 JavaScript 和 PHP SDK?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6147631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Facebook Login: How to combine JavaScript with PHP SDK?
提问by Antony
How do I go about combining examples from both
我如何结合两者的例子
https://github.com/facebook/php-sdk/blob/master/examples/example.phpand https://github.com/facebook/connect-js/blob/master/examples/jquery/login.html
https://github.com/facebook/php-sdk/blob/master/examples/example.php和 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
最终目标是让我们可以使用 jQuery 登录,并且以某种方式以下行仍然有效
$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)
我目前注意到的是,当我使用 JavaScript 登录时,它没有使用 PHP 设置会话,因此,它与基于 PHP 的 SDK 不兼容(因此在这种情况下 $user 仍然为空)
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!
我不是 的忠实粉丝<a href="$facebook->getLoginUrl()">Login with Facebook</a>
,因为它会导致浏览器加载不同的页面(facebook 登录),然后重定向回 Web 应用程序。如果我可以加载一个弹出框供用户登录,然后透明地重定向回我的网络应用程序,那就更理想了。因此,我将不胜感激有关如何实施此用户体验的任何建议。谢谢!
采纳答案by Phil
I just use the Facebook login button - https://developers.facebook.com/docs/reference/plugins/login/
我只使用 Facebook 登录按钮 - 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.
一旦用户使用它登录(如果他们还没有)并授予对您的应用程序(站点)的访问权限,PHP-SDK 使用 Facebook 会话和 API 应该没有问题。
Update
更新
I just tried this with the new version 3.0 PHP SDK and it is definitely notworking. Found this on the developer blog
我只是用新版本的PHP 3.0 SDK尝试这样做,这是绝对不工作。在开发者博客上找到了这个
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.
如果您使用 JavaScript SDK 与 PHP SDK 一起登录,您将需要等待 JavaScript SDK 升级(4 周后推出)。由于 cookie 格式发生变化,PHP SDK 3.0.0 版本将无法与当前的 JavaScript SDK 合作。
In other words, use the v2 PHP SDK until the JS SDK is updated.
换句话说,在 JS SDK 更新之前使用 v2 PHP SDK。
回答by Pedro Villa Verde
Not sure if you got this working. The current JS API has a configuration to save the cookie so the server may read it:
不确定你是否有这个工作。当前的 JS API 有一个配置来保存 cookie,以便服务器可以读取它:
FB.init({
appId : 'YOUR APP ID',
cookie : true
});
Now, in PHP, you can do the following to use the session from the JS:
现在,在 PHP 中,您可以执行以下操作以使用来自 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.
希望它有所帮助。
回答by thereds1106
it's sound like facebook didn't fix this problem at the right time(as they talk https://developers.facebook.com/blog/post/503).
听起来 facebook 没有在正确的时间解决这个问题(正如他们所说的https://developers.facebook.com/blog/post/503)。
this is newest infomation about js sdk https://developers.facebook.com/blog/post/525/
这是关于 js sdk 的最新信息 https://developers.facebook.com/blog/post/525/
回答by Rick Pastoor
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:
经过长时间的搜索,我发现这解决了我的问题,即 PHP SDK 没有接收 Facebook 会话。显然,Javascript SDK 没有以正确的方式设置会话。将此添加到您的事件auth.login
侦听器中:
FB.Event.subscribe('auth.login', function(response) {
FB._oauth = false;
FB.Cookie.setEnabled(true);
FB.Auth.setSession(response.authResponse, response.status);
FB._oauth = true;
window.location.reload();
});
Also, make sure you set the App Domain to match your cookie domain. After fixing these items, it finally worked.
此外,请确保您将应用程序域设置为与您的 cookie 域相匹配。修复这些项目后,它终于奏效了。
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.
编辑:今天早上,一切正常。当我刚刚测试更多时,我发现我必须编辑 base_facebook.php 以确保对 Facebook 的请求不会超时。我的猜测是,因为我正在本地机器上进行测试,所以连接速度不够快,无法及时连接。这解释了有时会起作用有时不起作用的奇怪行为。
Alter the connecttimeout in the $CURL_OPTS array on line 128:
在第 128 行更改 $CURL_OPTS 数组中的连接超时:
public static $CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 20,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'facebook-php-3.1',
);
回答by Chuck Le Butt
In version 4 of the SDK it works automatically with the JavaScript SDK. You just need to use FacebookJavaScriptLoginHelper
:
在 SDK 版本 4 中,它自动与 JavaScript SDK 一起工作。你只需要使用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'
))->execute()->getGraphObject(GraphUser::className());
print_r($user_profile);
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
No need to mess around with cookies!
无需乱搞饼干!