php 您如何获取 facebook 用户的信息并将其插入数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11642214/
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
How do you get a facebook user's information and insert it into a database?
提问by johnny
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.
我不知道如何问它,但我正在尝试自学如何创建一个使用图形 API 的程序。我看到的大部分教程都是旧的,我不知道它们现在的相关性如何。本质上,我试图获得某人点击我的应用程序的“东西”,它说,这个应用程序想要你的用户名等,然后允许或不允许。
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.
我很抱歉一个模糊的问题,我已经搜索过了。不要求你为我编写我的代码,只是指出我正确的方向,也许是一个现代教程来完成我的要求。谢谢。
回答by Oscar Jara
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.
仅供参考,此时您还可以为您的应用程序设置徽标、管理管理员并获取您稍后将在 PHP 文件中使用的应用程序 ID 和机密。
(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. 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).
5)上面的例子使用了没有 JavaScript 的 Facebook PHP SDK。因此,如果用户想要登录并授权您的应用程序获取信息,那么整个页面将被重定向到您应用程序的 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. Another difference is that a popup will appear instead of redirecting the whole page.
6)下面的代码将执行与上面相同的操作,但使用 JavaScript 和自定义 Facebook 登录按钮,允许您按照您在问题中所写的方式设置特殊权限。另一个区别是会出现一个弹出窗口,而不是重定向整个页面。
(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.
希望这有助于作为参考。
回答by uzyn
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.orgfor a quick demo and download the library which contains sample code.
请参阅http://opauth.org以获取快速演示并下载包含示例代码的库。
回答by Jo?o Mosmann
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.
请记住,在您的应用程序配置中,您必须指定您希望从用户那里获得的信息类型。
回答by ewein
You can use the php API and the query language that facebook provides (FQL). Here is a link that contains sample query's and tutorials http://developers.facebook.com/docs/reference/fql/
您可以使用 php API 和 facebook 提供的查询语言 (FQL)。这是一个包含示例查询和教程的链接http://developers.facebook.com/docs/reference/fql/

