Monday , 24 July 2017

How to Develop Facebook App Using PHP SDK

Develop Facebook App

Facebook App Development is one of the most entertaining thing that we can do on Facebook, yes we are stuck in groups, pages, friends, notifications etc so we all need something different like make Facebook App. So this text tutorial is all about make Facebook App for beginners. Actually I love to develop web apps with PHP and it is one of the most widely used programming language to make web applications and no need to mention here that Facebook, YouTube, Twitter etc all big sites are developed mostly in PHP so we are going to learn Facebook App Development in PHP if you know PHP then it is going to be great but if you don’t know this language that how to code then it’s ok you can still go on with this tutorial.

Get Web Hosting and Domain

The first thing you need is the web hosting and domain simply you can try free web hosting services for example hourb.com and 000webhost.com etc. After getting a web hosting account then the second thing you need to do is to verify your Facebook account, it is so simple I hope most of you have already verified it. Then after verifying the account just go to Facebook Developers to create your first Facebook App.

Facebook App Basic Info

Click on Create New App button on the top-right corner of your screen and fill the two boxes carefully the first one is about the title of your app and second  is namespace it will be used as username for your app just remember this and click on Continue and then you will be asked for security check just complete that security process and go on.

Create Facebook App

After security check you will be redirected to a page where you need to add basic info about your Facebook app. So here on this page first copy and save somewhere on your computer App ID and App Secret and also Namespace this info will be used late while developing the app. Then in the Canvas URL just put the URL where our app will pull content to show on Facebook as an iframe and in Secure Canvas URL it is the same as first one but here it starts from https:// it is for the users who are using secure browsing on Facebook and remember our website should have SSL certificate if you don’t have it then simply add the URL but it will not show anything but it will work properly with http:// then click on Save Changes so screen shot below will guide you more.

Facebook App Basic Info

Now after saving the information about your app now you need to upload Facebook PHP SDK files and an index file to your web hosting. I am not going to show you how to web hosting free or paid just Google it yourself however I am using free web hosting you can try 000webhost.com, hourb.com etc as written above.

Facebook PHP SDK

Now it’s time to download PHP SDK simply click here to download. Extract it and I use FileZilla FTP Client to upload files directly to web hosting and now just upload the whole src folder to your website and make you upload to the same URL directory which you used to add in Canvas URL option.

Develop Facebook App Using PHP

Now it’s time to upload the index file if you have uploaded PHP SDK files in the previous step. Just copy and paste the code into notepad which is given below:

<!DOCTYPE html>
<html>
<head>
<title>Your App Title</title>
</head>
<body><?php
//facebook application configuration
$fbconfig['appid' ] = "APP ID HERE";
$fbconfig['secret'] = "APP SECRET HERE";$fbconfig['baseUrl'] = "http://roomi.orgfree.com/etb/"; //canvas URL;
$fbconfig['appBaseUrl'] = "http://apps.facebook.com/et_blog/"; //facebook URL;/*
* If user first time authenticated the application facebook
* redirects user to baseUrl, so I checked if any code passed
* then redirect him to the application url
*/if (isset($_GET['code'])){
header("Location: " . $fbconfig['appBaseUrl']);
exit;
}

if (isset($_GET['request_ids'])){
//user comes from invitation
//track them if you need
}$user = null; //facebook user uid
try{
include_once "src/facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));//Facebook Authentication part
$user = $facebook->getUser();
// 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.$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email'
)
);

if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}

if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}

echo $user_profile['name'];

?>
</body>
</html>

  • Replace APP ID HERE with your App ID.
  • Replace APP SECRET HERE with your App Secret.
  • Replace et_blog with your app Namespace.
  • Replace http://roomi.orgfree.com/etb/ with your website URL where the SDK files are uploaded.

After changing just save the index file and rename it to index.php and upload to your website and make sure it is uploaded where src is uploaded make sure not in the src folder but outside it.

App is Ready

Now the app the is ready if you are familiar with PHP then you can make different apps however in future I’ll try to post some more advanced tutorials about making good Facebook apps but this tutorial is just for those who can create there own Facebook apps but stuck somewhere and have no knowledge where to start so now they are start it by the way this simple app only shows the name of the logged in user because of the last line of code in future as I said I’ll try to make video tutorials with advance Facebook App Development so have a nice day if you have any question then simply ask don’t hesitate it’s your right to ask anything about this post.

Leave a Reply