你如何获得一个Facebook用户的信息,并将其插入到数据库? [英] How do you get a facebook user's information and insert it into a database?

查看:583
本文介绍了你如何获得一个Facebook用户的信息,并将其插入到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道怎么问,但我想教我如何创建使用图形API的程序。大多数我见过的教程是年龄大了,我不知道他们现在怎么样相关。从本质上讲,我试图让一事一议里someons点击我的应用程序,它说,该应用需要您的用户名,等等,然后允许或不允许。

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. 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.

我希望它采取的信息,然后我可以将其插入到数据库中。我使用PHP和有一个域。

I want it to take the information and then I can insert it into a database. I am using php and have a domain.

我可以插入数据没有问题,如果我能得到的数据。我不明白怎么做了。

I can insert the data no problem, if I can get the data. I don't understand how to do it.

我是一个含糊不清的问题对不起,我已搜查。不要求你写我的code对我来说,只是点我朝着正确的方向发展,也许到现代的教程做什么我问。谢谢你。

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)创建Facebook应用程序从这里的 http://developers.facebook.com/apps
 并与您的域配置。这一步是很容易的,把你想,这将是您的应用程序名称的命名空间,那么请检查您的应用程序将被用来作为一个应用程序和登录页面(不扇页或一些相关的),最后指定的URL(离开画布URL空白),其中你会使用Facebook的API(我认为这个URL必须是HTTPS下,但我不知道为什么Facebook的剧照允许HTTP,所以不用担心)。

(1) Create a Facebook application from here 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 app name, then check that your app will be used as an application and login page (not fan page or something related) and finally specify the URL's (leave Canvas URL blank) in which you will use Facebook API (I think that this URL must be under HTTPS but I don't know why Facebook stills allowing HTTP, so don't worry).

登录配置:

只需在URL中设置: http://yourdomain.com/

Just set in the URL: http://yourdomain.com/

应用配置:

如: http://www.mydomain.com/myfacebookapp/

因此​​,当用户进入

http://apps.facebook.com/yourappName

意味着他/她是真正参观的第一个环节,并在该的index.php 则需要从下面做的一切。只知道,在这一点上,你也可以为您的应用程序标识,管理你的应用程序的管理员,让你的应用程序ID和秘密,你将需要在你的PHP文件中进一步使用。

Means that he/she is really visiting the first link and at that index.php you will need to do everything from below. Just to know, at this point you can also set a logo for your application, manage administrators of your app and get your application ID and Secret that you will need to use further in your PHP file.

(如果你在这一步,你可以做一个谷歌搜索迷茫,这样的配置也容易找到YouTube)的

(2)我一直用这个文件我的关系与Facebook API PHP环境,你可以从这里下载:<一href=\"https://www.dropbox.com/s/itw4pav1f7a9vez/files.rar\">https://www.dropbox.com/s/itw4pav1f7a9vez/files.rar

(2) I always use this files to relate my PHP enviroment with Facebook API, you can download it from here: https://www.dropbox.com/s/itw4pav1f7a9vez/files.rar

(3)把那些文件的文件夹名为FB。

(3) Put those files in a folder called fb.

(4)我会告诉你从一个用户的数据和图像的方式,但首先他/她通过得到登录到你的应用程序,让您的应用程序,从他/她获取信息。所以,在这个例子中,我将用一个简单的按钮登录。

(4) I will show you the way to get data and picture from a user but first he/she has to allow your app to get information from him/her by getting logged into you app. So, for this example I will use a simple button for the login.

(不要忘记,以取代您的应用程序ID和秘密 - XXX和YYY')

<?php

require 'fb/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'xxx',
  'secret' => 'yyy',
));

// Get User ID
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
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>
        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 ?>
</html>

(5)上面的例子使用Facebook的PHP SDK没有的JavaScript。因此,如果用户想要登录并授权您的应用程序得到他/她的信息,整个页面将被重定向到您的应用程序的Facebook的权限页,然后回到你的Facebook应用程序的主页面(在配置时指定您创建的应用程序)。

(5) The above example uses Facebook PHP SDK without javascript. So, if user wants to login and authorize your app to get his/her information, the whole page will be redirected to the Facebook permissions page of your app and then go back to the main page of your Facebook app (specified in the configuration when you created your app).

(6)下一个code会做上述相同,但使用JavaScript和自定义的Facebook登录按钮,让你把特殊权限,你在你的问题中写道。另一个不同之处在于一个弹出反而出现重定向整个页面的

(6) The next 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. Another difference is that a popup will appear instead of redirecting the whole page.

(不要忘记,以取代您的应用程序ID和秘密 - XXX和YYY')

<?php

require 'fb/facebook.php';

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

// See if there is a user from a cookie
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $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 with facebook button */
            FB.Event.subscribe('auth.login', function(response) {
              if (response.authResponse) {
                 // Specify the login page (the page in which your fb login button is situated)
                 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 this page
                    window.location = 'main.php';
                }
            }, true);
          }
        </script>
    </body>
</html>

(7)正如你所看到的,在Facebook的登录按钮scope属性决定了哪些特殊权限和信息,您的应用程序将需要像他/她的例如电子邮件用户(即始终私营)。

(7) As you can see, the scope attribute in the facebook login button determines which special permissions and information your app will need from a user like his/her email for example (that is always private).

(8)我想补充的东西,你可以从你使用以下code知道他/她的ID有人只得到公共信息:

(8) Just to add something, you can get only public information from someone you know his/her id using the following code:

(假设你的Facebook朋友的页面是: http://www.facebook.com/foobar 或类似: <一href=\"http://www.facebook.com/users/profile/1002020300010\">http://www.facebook.com/users/profile/1002020300010)

(Supposing your facebook friend's page is: http://www.facebook.com/foobar or like: http://www.facebook.com/users/profile/1002020300010)

//For example: your facebook friend's page is http://www.facebook.com/foobar
$myFriend = $facebook->api('/foobar');
//For example: your facebook friend's page is http://www.facebook.com/users/1002020300010
$myFriend = $facebook->api('/1002020300010');
//Print information like your friend's name:
echo $myFriend['name'];
//Print all information captured:
print_r($myFriend);

和让你的朋友的图片:

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

或者

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


最后,假设你有变量捕获的所有用户信息,您可以将所有的数据到数据库表没有问题的。


Finally, supposing that you have all user information captured in variables, you can insert all data to a database table without problems.

希望这有助于: - )

Hope this helps :-)

这篇关于你如何获得一个Facebook用户的信息,并将其插入到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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