简体   繁体   English

您如何获取 Facebook 用户的信息并将其插入数据库?

[英]How do you get a facebook user's information and insert it into a database?

I wasn't sure how to ask it, but I am trying to teach myself how to create a program that uses the graph api.我不确定如何提问,但我正在尝试自学如何创建一个使用图形 API 的程序。 Most of the tutorials I have seen are older, and I don't know how relevant they are now.我看到的大多数教程都比较老,我不知道它们现在的相关性如何。 Essentially, I am trying to get the "thing" where someons clicks my app, it says, this app wants your username, etc. and then Allow or Don't Allow.本质上,我试图获得有人点击我的应用程序的“东西”,它说,这个应用程序需要你的用户名等,然后允许或不允许。

I want it to take the information and then I can insert it into a database.我希望它获取信息,然后我可以将其插入数据库。 I am using php and have a domain.我正在使用 php 并拥有一个域。

I can insert the data no problem, if I can get the data.如果我可以获取数据,我可以毫无问题地插入数据。 I don't understand how to do it.我不明白该怎么做。

I'm sorry for a vague question, and I have searched.对于一个模糊的问题,我很抱歉,我已经搜索过了。 Not asking you to write my code for me, just point me in the right direction, maybe to a modern tutorial doing what I am asking.不要求您为我编写代码,只是为我指出正确的方向,也许是一个现代教程来完成我所要求的。 Thanks.谢谢。

1) Create a Facebook application from here: 1)从这里创建一个 Facebook 应用程序:

http://developers.facebook.com/apps http://developers.facebook.com/apps

And configure it with your domain.并使用您的域配置它。

This step is really easy, put any namespace you want that will be your application name, then check that your application will be used as an "application and login page" (not a fan page or something related) and finally specify the URLs where you will use Facebook API (leave Canvas URL blank).这一步真的很简单,把你想要的任何命名空间作为你的应用程序名称,然后检查你的应用程序是否将用作“应用程序和登录页面”(不是粉丝页面或相关的东西),最后指定你所在的 URL将使用 Facebook API(将 Canvas URL 留空)。

Just a heads up, I believe Facebook API requires HTTPS URLs but I don't know why is still allowing HTTP, so don't worry by now.请注意,我相信 Facebook API 需要 HTTPS URL,但我不知道为什么仍然允许 HTTP,所以现在不用担心。

Login config:登录配置:

Set the URL: http://yourdomain.com/设置网址:http: //yourdomain.com/

Application config:应用配置:

http://yourdomain.com/myfacebookapp/ http://yourdomain.com/myfacebookapp/

So, when a user goes to:因此,当用户转到:

http://apps.facebook.com/yourappName http://apps.facebook.com/yourappName

Means that a user is really browsing the first link and in that page (let's say index.php ) you will need to do everything from below.意味着用户实际上正在浏览第一个链接,并且在该页面(比方说index.php )中,您需要从下面开始执行所有操作。

Just FYI, at this point you can also set a logo for your application, manage administrators and get your application ID and secret that you will use in your PHP file later.仅供参考,此时您还可以为您的应用程序设置徽标、管理管理员并获取您的应用程序 ID 和密码,稍后您将在 PHP 文件中使用这些信息。

(If you get confused in this step you can do a Google search, this configuration is easy to find) (如果你对这一步感到困惑,你可以谷歌搜索,这个配置很容易找到)

2) I always use these files to link my PHP enviroment with Facebook API, here's the link from my Dropbox: https://www.dropbox.com/s/itw4pav1f7a9vez/files.rar 2)我总是使用这些文件将我的 PHP 环境与 Facebook API 链接起来,这是我的 Dropbox 的链接: https ://www.dropbox.com/s/itw4pav1f7a9vez/files.rar

3) Put those files in a folder called fb . 3)将这些文件放在名为fb的文件夹中。

4) I will show you the way to get data and picture from a user but first the user has to allow your application to get this info when getting logged in your application. 4)我将向您展示从用户获取数据和图片的方法,但首先用户必须允许您的应用程序在登录您的应用程序时获取此信息。

So, for this example I will use a simple button for the login:因此,对于此示例,我将使用一个简单的按钮进行登录:

(Don't forget to replace your application ID and secret, notice the 'xxx' and 'yyy') (不要忘记替换您的应用程序 ID 和密码,注意 'xxx' 和 'yyy')

<?php

require 'fb/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxx',
  'secret' => 'yyy',
));

// Check if user is already logged
$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Get login or logout URL
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

?>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>Facebook PHP SDK</title>
  </head>
  <body>
    <h1>Facebook PHP SDK</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        <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>Your picture</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

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

5) Above example uses Facebook PHP SDK without JavaScript. 5)上面的例子使用了没有 JavaScript 的 Facebook PHP SDK。 So, if user wants to login and authorize your application to get the info then the whole page will be redirected to the Facebook permissions page of your application and after it will go back to the main page of your Facebook application (specified in the configurations from your application).因此,如果用户想要登录并授权您的应用程序获取信息,那么整个页面将被重定向到您应用程序的 Facebook 权限页面,然后它将返回到您的 Facebook 应用程序的主页(在配置中指定来自你的申请)。

6) Below code will do the same as above but using JavaScript and custom Facebook login button that allows you to put special permissions as you wrote in your question. 6)下面的代码将执行与上面相同的操作,但使用 JavaScript 和自定义 Facebook 登录按钮,允许您按照您在问题中所写的那样设置特殊权限。 Another difference is that a popup will appear instead of redirecting the whole page.另一个区别是将出现一个弹出窗口而不是重定向整个页面。

(Don't forget to replace your application ID and secret, notice the 'xxx' and 'yyy') (不要忘记替换您的应用程序 ID 和密码,注意 'xxx' 和 'yyy')

<?php

require 'fb/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxx',
  'secret' => 'yyy',
));

// Check if user is already logged
$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
    $logoutUrl = $facebook->getLogoutUrl();
  } catch (FacebookApiException $e) {
    $user = null;
  }
} else {
    $loginUrl = $facebook->getLoginUrl();
}

?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Facebook PHP SDK</title>
    </head>
    <body>
        <fb:login-button size="small" onlogin="after_login_button()" scope="email, user_about_me, user_birthday, user_status, publish_stream, user_photos, read_stream, friends_likes">Login with facebook</fb:login-button>
        <div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function() {
            FB.init({
              appId: '<?php echo $facebook->getAppID() ?>',
              cookie: true,
              xfbml: true,
              oauth: true
            });
            
            // This is used by Facebook login button
            FB.Event.subscribe('auth.login', function(response) {
              if (response.authResponse) {
                 // Specify the login page where Facebook login button is located
                 window.location = 'main.php';
              }
            });
            FB.Event.subscribe('auth.logout', function(response) {
                window.location = 'logout.php';
            });
          };
          (function() {
            var e = document.createElement('script'); 

            e.async = true;
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';

            document.getElementById('fb-root').appendChild(e);
          }());
          
          function after_login_button(){
            FB.getLoginStatus(function(response) {
                if (response.status == 'connected') {
                    // If user is connected, redirect to below page
                    window.location = 'main.php';
                }
            }, true);
          }
        </script>
    </body>
</html>

7) As you can see, the scope attribute in the Facebook login button determines which special permissions and info your application will need from a user like the email for example (which is always private unless authorized). 7)如您所见,Facebook 登录按钮中的 scope 属性决定了您的应用程序需要来自用户的哪些特殊权限和信息,例如电子邮件(除非授权,否则始终是私有的)。


8) To add something, you can get only public information from someone by using the following: 8)要添加一些东西,您可以使用以下方法仅从某人那里获取公共信息:

// For example: Your Facebook friend's profile is http://www.facebook.com/foobar
$myFriend = $facebook->api('/foobar');
// For example: Your Facebook friend's profile is http://www.facebook.com/users/1002020300010
$myFriend = $facebook->api('/1002020300010');
// Print the name
echo $myFriend['name'];
// Print all data
print_r($myFriend);

And, in order to get your Facebook friend's picture just do this:而且,为了得到你的 Facebook 朋友的照片,只需这样做:

<img src="https://graph.facebook.com/foobar/picture">

Or:要么:

<img src="https://graph.facebook.com/1002020300010/picture">

Finally, supposing that you have all user info that was needed then now you can save it all into DB without problems or restrictions.最后,假设您拥有所有需要的用户信息,那么现在您可以毫无问题或限制地将其全部保存到数据库中。

Hope this helps as reference.希望这有助于参考。

It seems that you are looking to creating an authentication (login) using Facebook.您似乎希望使用 Facebook 创建身份验证(登录)。

You may wish to check out Opauth , it handles all of that for you and returns you the user info in an array, of which you can then insert into database easily.您可能希望查看Opauth ,它会为您处理所有这些,并以数组形式返回给您用户信息,然后您可以轻松地将其插入数据库。

See http://opauth.org for a quick demo and download the library which contains sample code.请参阅http://opauth.org获取快速演示并下载包含示例代码的库。

Considering that you are using the PHP SDK Classes provided by Facebook here考虑到您在这里使用 Facebook 提供的 PHP SDK 类

You can do something like this你可以这样做

$fb = new Facebook(array(
     'appId' => 'YOUR_APP_ID',
     'secret' => 'YOUR_APP_SECRET'
));


$userData = $fb->api('/me','GET');
$userData = serialize($userData);

//now userdata is a string, so, insert into the database.

Remembering, in your app configurantion, you must specify the kind of information you want from user.请记住,在您的应用程序配置中,您必须指定您希望从用户那里获得的信息类型。

You can use the php API and the query language that facebook provides (FQL).您可以使用 php API 和 facebook 提供的查询语言 (FQL)。 Here is a link that contains sample query's and tutorials http://developers.facebook.com/docs/reference/fql/这是一个包含示例查询和教程的链接http://developers.facebook.com/docs/reference/fql/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Facebook PHP API获取用户信息 - How to get user information with Facebook PHP API 如何使用php将用户信息从facebook sdk提取到我网站的数据库中 - how to pull user information from facebook sdk into my site's database using php 如何使用Facebook Graph API获取用户的网络信息? (PHP) - How to get user's network information using Facebook Graph API? (PHP) 如何在Android应用程序的服务器端(PHP)中从Facebook获取用户信息 - How to get user's information from Facebook in the server side (PHP) in an Android application 如何使用CodeIgniter 3.1.0将Facebook用户基本信息保存到数据库 - How to save Facebook user basic information to database with CodeIgniter 3.1.0 如何获取要插入数据库的iFrame内容? - How to you get the contents of an iFrame to insert into a database? 如何从表单中获取信息以使用XAMPP插入mySQL数据库? - How do I get the information from my form to insert into a mySQL database using XAMPP? Facebook API-如何通过Facebook API获取Facebook用户的个人资料图片(无需用户“允许”应用程序) - Facebook API - How do I get a Facebook user's profile image through the Facebook API (without requiring the user to “Allow” the application) 如何询问 Facebook 用户的私人信息,如电子邮件等 - How to ask for Facebook user's private information like email, etc 您如何在Facebook图表上获得给定用户的最新帖子? - How do you get the most recent posts from a given user on Facebook graph?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM