How do you get a facebook user's information and insert it into a database?
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 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).
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.
Login config:
Set the URL: http://yourdomain.com/
Application config:
http://yourdomain.com/myfacebookapp/
So, when a user goes to:
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.
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.
(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
3) Put those files in a folder called 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.
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')
<?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).
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.
(Don't forget to replace your application ID and secret, notice the 'xxx' and '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).
8) To add something, you can get only public information from someone by using the following:
// 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:
<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.