We don’t need to talk much about facebook itself. Let us get straight to it.
This tutorial explains how you can develop a basic facebook application using Facebook Open Graph API and PHP SDK.
We’ve also given a sample application code written in PHP using facebook API, which you can use as a starting point to develop your application.
Step 1: Set up Application with your Facebook Account
Login to your facebook account and navigate to the facebook developers page. Or, you can go directly to developers.facebook.com.
From the developer’s page, click on “Create App” option under the “Apps” menu items as shown below.
In the create new app screen, enter the name to your application, optionally make a name-space for it and choose the category for your application.
Click on “Create App” to create the application. This will take you to the application dashboard from where you can see all the settings for the new app you just created.
From the Dashboard tab note down your “AppID” and “App Secret”, which you will need in further steps to authenticate and fetch the data using the Open Graph API calls. From here you can also view the “User Stats” and the “API Stats” for the application.
From the settings tab, click on “Add Platform”, from here select “Website” as we are going to develop a facebook web app.
From here, set your website url and mobile site url. In the settings menu, go to “Advanced” tab where you can set the EDeauthorize Callback URL”, implement the restrictions, implement the security measure like IP white list, email notification, Oauth redirect URI and control the Insights setting for the analytics of your application.
You need to make your application public once it is ready to go live. You can do it from the “Status and Review” menu as shown below.
Step 2: Develop Your App using Facebook PHP SDK and Open Graph API
You can download facebook php SDK from here.
Like Us: While we are on the topic of facebook, like The Geek Stuff on facebook
Extract it to the root of your web server, we’ll be adding “require ../src/facebook.php” inside the application code that we are going to develop.. For example consider the script below:
<?php require '../src/facebook.php'; /* Create our Application instance (replace this with your appId and secret). */ $facebook = new Facebook(array( 'appId' => '140978689667076108429', 'secret' => 'f6353453e5b4d6daacsfa63e0sdfasfc93c', )); /* You application logic goes here */ ?>
You can explore the graph api from here. Facebook have a utility called “Graph API explorer” which can be very handy while developing such applications.
The following sample application code uses facebook PHP SDK. Go through the inline comments to understand the flow. Here we are using graph api to pull the user’s profile and his friend list object.
<?php require '../src/facebook.php'; // Creating our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => '1408167076108429', 'secret' => 'f6057e5b4d6daac9950863e0b588c93c', )); // Getting User ID $user = $facebook->getUser(); // Get Access token $access_token = $facebook->getAccessToken(); // We may or may not have this data based on whether the user is logged in. // // If we have a $user id here, it means we know the user is logged into // Facebook, but we don't know if the access token is valid. An access // token is invalid if the user logged out of Facebook. if ($user) { try { // Proceed knowing you have a logged in user who's authenticated. // Retrieving user's friend list using fb graph api $user_friendList = $facebook->api('/me/friends?access_token='.$access_token); $user_profile = $facebook->api('/me','GET'); } 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 { $statusUrl = $facebook->getLoginStatusUrl(); $loginUrl = $facebook->getLoginUrl(); } ?> <!doctype html> <html> <head> <title>php-sdk</title> <style> body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; } h1 a { text-decoration: none; color: #3b5998; } h1 a:hover { text-decoration: underline; } </style> </head> <body> <h1>Sample web app using facebook php SDK </h1> <?php if ($user): ?> <a href="<?php echo $logoutUrl; ?>">Logout</a> <?php else: ?> <div> Check the login status using OAuth 2.0 handled by the PHP SDK: <a href="<?php echo $statusUrl; ?>">Check the login status</a> </div> <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> Welcome <?php echo $user_profile['name']; ?> !!! </h3> <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"> <h3>Your friend list Object is as follows (/me/friends?token=<?php echo $access_token; ?>)</h3> <pre><?php print_r($user_friendList); ?></pre> <?php else: ?> <strong><em>You are not Connected.</em></strong> <?php endif ?> </body> </html>
There you have it. That is how you develop a basic facebook app. If you’ve been thinking about developing a facebook app, the above code should give you a great jumpstart.
Comments on this entry are closed.
This tutorial is good but since facebook has changed it’s php sdk to 4. I’m having difficulty. Can you make a tutorial on php sdk 4 for facebook for login user and extrating user information along with email and photo and then log out users.
Thanks
but i m nt access the friend list,,it shows only my profile
Fatal error: Call to undefined method Facebook::getLoginStatusUrl() in /home/erickrmz/public_html/redsocial/test.php on line 40
hey buddy ithink this is old version tutorial…can you please write one for newest..?
Fatal error: Call to undefined method Facebook::getLoginStatusUrl()
i think it is useful for old version of sdk write new one….
NOT ABLE TO PUBLISH MESSAGE TO FACEBOOK (USING GRAPH API EXPLORER TOOL)