使用facebook php-sdk(v.3.0.0)结合使用会话和cookie的facebook登录 [英] Incorporating facebook login, using facebook php-sdk (v.3.0.0), with sessions and cookies

查看:75
本文介绍了使用facebook php-sdk(v.3.0.0)结合使用会话和cookie的facebook登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,并且很难理解Facebook登录系统.

I am new to PHP, and have had a very difficult time understanding the facebook login system.

我已经从github(https://github.com/facebook/php-sdk/)下载了三个src/文件.我尝试使用example.php文件来入门.但是,我不确定该怎么办.

I have downloaded the three src/ files from github (https://github.com/facebook/php-sdk/). I tried using the example.php file to get me started. However, I am not sure what to do with it.

对于那些不熟悉该文件的人,这里是example.php的副本,其中删除了一些样式:

For those who are unfamiliar with the file, here is a copy of example.php, with some of the styling removed:

require '../src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => '...',
  'secret' => '...',
));
$user = $facebook->getUser();
if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>

    <h3>Public profile of Naitik</h3>
    <img src="https://graph.facebook.com/naitik/picture">
    <?php echo $naitik['name']; ?>
  </body>
</html>

以下是我对此的疑问:

1)cookie怎么样?-我希望用户在重新打开浏览器后能够登录到我的网站.

1)What about cookies?--I want the user to be able to be logged into my website after re-opening his/her browser.

2)我需要退出example.php文件以验证/注册用户,开始会话,将会话存储在cookie中,获取用户的fb用户ID,fb名称,fb的最低要求是什么?图片以及fb朋友列表?

2)What is the bare minimum I need to get out of this example.php file to validate/register a user, begin a session, store the session in a cookie, get the user's fb user id, fb name, fb picture, and list of fb friends?

3)在src/文件中,有一个文件fb_ca_chain_bundle.crt",而我完全不知道这样的文件是什么,我不确定是否有必要.它的目的是什么?

3)In the src/ files, there is one 'file fb_ca_chain_bundle.crt,' and I am completely unfamiliar with what such a file, and I am not sure if it is even necessary. What is its purpose?

4)行$naitik = $facebook->api('/naitik');是此人的用户名"naitik"-因此,如果我输入facebook.com/naitik,它将显示他的公开个人资料?将"/naitik"替换为"/me",将获得登录Facebook的人的公开个人资料?

4)The line $naitik = $facebook->api('/naitik'); is "naitik" the username of this person--so if I type facebook.com/naitik it will show his public profile? is replacing "/naitik" with "/me" what will get the public profile of the person logged into facebook?

5)如何获取访问令牌,以及如何在代码中使用它?

5)How do I get the access token, and how do I use it in my code?

6)当我为用户创建会话和cookie以便重新打开浏览器后登录用户时,会话和cookie中应该确切存储什么?

6)When I create a session for the user, and a cookie so that the user is logged in after reopening the browser, what should I exactly be storing in my sessions and cookies?

我知道这是很多问题,但是我在线上浏览了许多教程,但都没有一个很好的解释.主要是因为它们只是链接回Github PHP-SDK文件.另外,其中大多数解释了PHP-SDK的早期版本. 如有任何疑问,我们将不胜感激.

I know this is many questions, but I have looked through many tutorials online, and none of them have done a good job explaining this, mostly because they just link back to the Github PHP-SDK files. Plus, most of them explain a previous version of PHP-SDK. Any help is appreciated, with any of the questions.

推荐答案

回答您的问题

1)饼干呢?

您只需在Facebook初始化中添加一个参数即可. 将其更改为以下

You just add a parameter to the Facebook initialization. Change it to the following

$facebook = new Facebook(array(
  'appId'  => '...',
  'secret' => '...',
  'cookie' => true,
));

2)我需要从这个example.php文件中找到的最低要求是什么?

2)What is the bare minimum I need to get out of this example.php file to ....

在此示例中,并非您想要的所有内容.代码的上半部分显示了如何连接和验证用户.后半部分仅转储其基本详细信息和naitik的详细信息.对于其余部分,您需要进一步研究.

Not everything you want is in this example. The top half of the code shows you how to connect and validate a user. The second half just dumps out their basic details and naitik's details. For the rest you need to look further.

3)在src/文件中,有一个文件fb_ca_chain_bundle.crt",

3)In the src/ files, there is one 'file fb_ca_chain_bundle.crt,'

此文件的目的是为CURL错误60提供一种解决方法.请阅读以下内容:

The purpose of this file is to offer a workaround for CURL error 60. Read this:

http://www.takwing.idv.hk/blog/2011/php-sdk-demystified-%E2%80%93-how-curl-error-60-is-handled/

4)$ naitik行= $ facebook-> api('/naitik');是"naitik"这个人的用户名-因此,如果我输入facebook.com/naitik,它将显示他的公开个人资料?将"/naitik"替换为"/me",将获得登录Facebook的人的公开个人资料?

4)The line $naitik = $facebook->api('/naitik'); is "naitik" the username of this person--so if I type facebook.com/naitik it will show his public profile? is replacing "/naitik" with "/me" what will get the public profile of the person logged into facebook?

完全正确

5)如何获取访问令牌,以及如何在代码中使用它?

5)How do I get the access token, and how do I use it in my code?

$ facebook-> getAccessToken();

$facebook->getAccessToken();

您在其中添加了一些方法调用,但这并不是所有操作都必需的.

You add it some of the method calls, but it is not necessary for everything.

6)当我为用户创建会话和cookie以便重新打开浏览器后登录用户时,会话和cookie中应该确切存储什么?

6)When I create a session for the user, and a cookie so that the user is logged in after reopening the browser, what should I exactly be storing in my sessions and cookies?

Facebook SDk将解决此问题.您只需要存储有关您的App所需用户的任何其他信息.

The Facebook SDk is going to take care of that. You will just need to store whatever extra information about the user your App requires.

这篇关于使用facebook php-sdk(v.3.0.0)结合使用会话和cookie的facebook登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆