Hello there. I recently created a facebook web app that lets you to post to multiple groups, pages and your profile with just one click. You can post a message and even a link. Sounds cool right ?
You can find the app here. And so I surfaced a lot through the facebook php sdk while creating the app. So now I thought of writing a series of posts dealing with the facebook php sdk.
In this post I'll be dealing with how to login to a facebook account using the facebook php sdk and how to retrieve the user information. And so lets get started.
VIDEO TUTORIAL
Prerequisites :
You need to have the following :
- Fair knowledge on PHP
- Facebook PHP SDK (We will be downloading this)
- Your Text editor
Creating the app :
The first step is to create the actual facebook app. And this will be a kind of a communication link between facebook and your site. Follow the steps below to create your app :
1. Go to
Facebook Developers Page.
2. Click on apps on the header and go to
Register as a Developer.
3. And once you have registered as a developer you will get into the documentation. (I swear Facebook has a really bad documentation)
4. Click on the same apps tab and click on
Create a new app.
5. Enter a name, choose a category and proceed with creating the app.
6. In your app dashboard go to the settings tab and enter a contact email address.
7. Click on
Add Platform and select
Website.
8. In the site URL text box type in the domain of your site if you have one else type in localhost if you would be testing the app at your localhost. (Note : Facebook PHP SDK code would work only in the site which you mention here)
9. Save it return to your dashboard.
10. Make a note of your
App ID and
App Secret.
Bringing in the Facebook Login :
And now that you have completed creating the app and it is time to creating the login functionality. Follow the steps below to do so.
1. Download the
Facebook PHP SDK.
2. Unzip it and copy the
src folder inside it to the root of your app.
3. Create an
index.php file where you placed the src folder.
4. Fire up your
index.php file and type in the following code.
<?php
require_once 'src/facebook.php';
$fb = new Facebook(array(
'appId' => 'YOUR APP ID',
'secret' => 'YOUR APP SECRET ID'
));
Remember to replace your app id and your app secret. What this basically does is it creates an instance of the class BaseFacebook present in the facebook.php file.
5. And now to login the user add the following code.
if($fb->getUser() == 0){ //checks if the user is already logged in
$loginUrl = $fb->getLoginUrl(); //carries out if user not logged in
echo "<a href = '$loginUrl'>Login</a>";
}
else{
echo "<a href = 'logout.php'>Logout</a>"; //we will be creating the logout.php file later
}
In this piece of code we are letting the user to login to your site using his facebook account. We are displaying the login link if the user is not logged in else we are displaying the logout url.
6. We have completed logging in the user and now the job is to display the profile picture and name of the user. To do add the following code in the else part.
$api = $fb->api('me');
$name = $api[first_name] . " " . $api[last_name];
$id = $api[id];
echo "<img src = 'http://graph.facebook.com/$id/picture?width=150&height=150'><br>";
echo "Hi $name";
7. And so the only job left is to create the
logout.php file. Create a file named logout.php and add the following code in it.
<?php
require_once 'src/facebook.php';
$fb = new Facebook(array(
'appId' => 'YOUR APP ID',
'secret' => 'YOUR APP SECRET ID'
));
$fb->destroySession();
?>
Replace your app id and the app secret in the respective places.
And that's it ! We have successfully created a facebook which lets the user to login and we have displayed the user's profile picture and his name ! We are done for the tutorial. Stay tuned for more tutorials on Facebook PHP SDK. Happy Coding :D