adbrite

Sunday, April 10, 2011

How to create a php facebook application using the facebook php client api (FBML type)



Many people are curious about how to connect to the facebook api with php and so that’s what this blog post is all about. This quick tutorial will connect you to facebook using php and the facebook api in minimal time.
There are five main steps for when you want to create a facebook application using the Facebook PHP client API library. Below is some working source code that you can use to get started right away.
1. Sign up for the facebook developer application, Facebook Developer app
2.Setup a new application by clicking on the “Set up a New Application”. The application fields change position quite a bit so you’ll have to hunt around to set it up as follows.
  • Setup the app Render method to be FBML type
  • Setup the application type to be Web type instead of the other type (Desktop type)
  • Setup your Canvas Page URL which is the part you add onto the end of http://apps.facebook.com/YOUR APP NAME HERE/
  • Setup your Canvas Callback URL which is the full web address to the directory on YOUR server which contains your index.php file (This will become your facebook app)
  • Take note of both of your Canvas URLs above as well as your API key & your Secret key because you will probably be needing them during your development. (The facebook app also displays this info which you can view at any time)
3. We can put simple FBML into your index.php file and then visit your Canvas Callback URL to see if it loads up fine. Be sure this works before proceeding further.
1<fb:name uid="loggedinuser" useyou="false">
4. Download the Facebook PHP client library and move it onto your server alongside your index.php file and make sure it works by using the following PHP code. If you get errors, then be sure your referencing the right directory(on linux systems, this is case sensitive). The facebook php client library can be downloaded from, Official facebook PHP client library
1require_once("directoryName/facebook.php");
5. Once you have the above code working, you can now proceed to use the following code that will test your facebook PHP client library to make sure it is also working.
01
02//include the PHP Facebook Client Library to help with the API calls and make life easy
03require_once("facebook-platform/php/facebook.php");
04 
05$apiKey "APIKEYHERE";
06$secretKey "SECRETKEYHERE";
07 
08//initialize the facebook API with your application API Key and Secret
09$facebook new Facebook($apiKey$secretKey);
10 
11//require the user to be logged into Facebook before using the application. If they are not logged in they will first
12//be directed to a Facebook login page and then back to the application's page. require_login() returns the user's
13//unique ID which we will store in fb_user
14$fb_user $facebook->require_login();
15 
16//echo '
Debug:', print_r($facebook,true), '
';
17?>
18 
19Hello '' useyou='false'possessive='true' />
The require_login method will check to see if your user is logged into facebook. So if someone tries to visit your application directly by visiting http://apps.facebook.com/YOUR APP NAME HERE, it will prompt them to login. This method returns the id of the of the logged in user and stores it in $fb_user. We can do a lot of things with this user id. Make certain to keep your $facebook variable which gives us complete access to the facebook API.
Important Note: It’s been reported that when you use the above code and visit your application coming from http://apps.facebook.com/YOUR APP NAME HERE, it works great. However some people have noticed that the page loads continuously and seemingly endlessly if you visit the page directly on your server. Luckily, there is a quick fix if statement that simply checks if the $_POST["fb_sig_in_canvas"] is set or not and if it isn’t set, we wont bother loading up anything related to facebook since that most probably means that user didn’t arrive from facebook.
Below is the finalized code that has a quick check that should resolve your problems. You’ll notice I also moved all of the code into the if statement so that you should get a blank page if you arrive from outside of facebook somehow. Of course there are ways around this, but the whole point is to prevent the page reloading over and over issue.  You can learn more about the variables that are available in the POST of your canvas callback page by vising the Facebook developer wiki page Your Callback page and you
01
02if(isset($_POST["fb_sig_in_canvas"])) {
03//if the fb_sign_in_canvas isset, then most probably, this user came from facebook
04//include the PHP Facebook Client Library to help with the API calls and make life easy
05require_once("facebook-platform/php/facebook.php");
06 
07$apiKey "APIKEYHERE";
08$secretKey "SECRETKEYHERE";
09 
10//initialize the facebook API with your application API Key and Secret
11$facebook new Facebook($apiKey$secretKey);
12 
13//require the user to be logged into Facebook before using the application. If they are not logged in they will first
14//be directed to a Facebook login page and then back to the application's page. require_login() returns the user's
15//unique ID which we will store in fb_user
16$fb_user $facebook->require_login();
17 
18//echo the Hello message
19echo "Hello $fb_user "' useyou='false' linked='false' />";
20 
21//you can debug the $facebook variable if you want to
22//echo '
Debug:', print_r($facebook,true), '
';
23}
24//uncomment these to display the pages $_POST variables if your curious to know about them
25//echo "
";
26//echo print_r($_POST);
27//echo "";
28?>
Important Note: You can only store the uid in your own servers database(so that you can recognize a returning user). We stored our uid in the $fb_user above. If your interested to know why you cannot store other data or if you just want to read more about this policy, check out facebook’s storable data policy.

Monetize your Website or Blog with BidVertiser

4 comments:

That's a great tutorial. We are now enjoying this social media site because of the Facebook Application Developer that making a useful applications. It is now getting interactive and easy to use.

Are you trying to make cash from your websites or blogs with popup ads?
In case you do, did you try using Clickadu?

Certainly! Connecting to the Facebook API using PHP is a shared aspiration, and I'm here to guide you through the process. In this blog post, we'll provide a concise and effective tutorial on using PHP and the Facebook API to establish a connection with Facebook. This quick and straightforward approach allows you to seamlessly integrate Facebook functionality into your projects. Whether you're a seasoned developer or someone eager to explore the possibilities, this guide is designed to streamline the process and get you connected to the Facebook API in the shortest possible time. If you're considering pay someone to take my course and master this connection, rest assured that our tutorial will set you on the right path for a smooth Facebook integration. Let's embark on this educational adventure together!

Post a Comment

thank you for visiting our site..!

clicksor

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More