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 ho
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')
'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();
}
?>
Facebook PHP SDK
Facebook PHP SDK
Logout
PHP Session
Your picture
Your info (/me)
You are not connected.
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')
'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();
}
?>
Facebook PHP SDK
Login with facebook
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:
Or:
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.