Facebook Is The New Black.
Everyone is talking about Facebook and their new Application Platform:
- Techcrunch - Facebook Launches Facebook Platform; They are the Anti-MySpace
- Mashable - Facebook Powertools: 150+ Apps, Scripts and Add-ons for Facebook
- Digg - The Impact of Facebook’s Platform
- Mashable - Facebook Platform: 30+ Awesome Applications for Facebook
- Valleywag - Developers, beware. Facebook really is the new Microsoft.
- Facebook’s Blog Post on the Facebook Platform
- Inside Facebook - Hottest Facebook Apps: July 28th
And I have to admit I think it’s pretty cool too. So I decided to create an application that uses the Facebook Platform. I’m writing the application in PHP and I thought it might be useful for others to know how to write their own Facebook applications with PHP. So here we are.
What Will I Learn?
I will cover the basics of using the Facebook PHP library and how to get your application started, including:
- Installing The Facebook Developer Application
- Downloading The Facebook PHP Client Library
- Creating Your Application
- Hello Facebook! Example
- FBML - Facebook Markup Language
- Using the Facebook API
Getting Started As A Facebook Developer
The first thing you need of course is a Facebook account. You can sign up for Facebook at www.facebook.com. Once you have your account you’ll need to install the Facebook developer application. This little tool will allow you to generate your application profile and get an API key (more on that later). Once you’ve logged into Facebook visit: www.facebook.com/developers/ or click below to install the developer application:
Add The Facebook Developer Application.
PHP Facebook API Client Library
Facebook has created a nice php library that allows you to use their API without writing a lot of extra code. You can grab the PHP version of the library at developers.facebook.com/resources.php. Download the ‘PHP (4 and 5) Client Library’.
Once you’ve downloaded the library unzip it into a folder that is accessible by your PHP scripts. So you would have something like /php_include_directory/facebook/ and in that folder you will have the entire Facebook PHP Client Library (3 folders: client, footprints, php4client). I’m using PHP5 so my examples will be using the “client” directory of the library. The footprints folder is an example application.
Creating Your Application Profile And API Key
Facebook requires that you register each application you make. Once you’ve logged into Facebook and installed the developer application go to the developer panel (or click here). Inside the developer application click “Set Up New Application”.
Choose a name for your application. This is important because it’s what users will see when they are browsing the application directory. Currently the name field is the only thing used when searching for applications. So it’s doubly important at this point.
Click on “Optional Fields”. Then fill out the Callback Url with the location of your script. This is the public URL on your webserver where the Facebook application will be.
Next fill out the “Canvas Page URL”. This is your application URL within Facebook. For example if the application was called “Makebeta Is Cool” then the application URL could be: “makebeta” which would make the full URL: http://apps.facebook.com/makebeta/.
Check Yes for Can your application be added on Facebook?
You should check the Developer Mode checkbox so that no one can add your application until you are done working on it.
Under Integration Points fill out Side Nav URL with the full Canvas Page URL. In the example above it would be http://apps.facebook.com/makebeta/. This allows users to add your application to their Facebook left side bar navigation.
All of these settings can be changed after the application has been created. But it’s a good idea not to change the Canvas Page URL or Name once you have users that have installed your application.
Further Reading: Starting your First Facebook App: Demystifying Application Form Field by Field
Get the API Key and Secret. You should now see your application listed with a unique API Key and Secret code. You’ll use these within your application.
Hello Facebook!
Let’s create a really simple first application that just says hello to the current Facebook user. Here’s the code for the Hello Facebook! application:
<?php
/* include the PHP Facebook Client Library to help
with the API calls and make life easy */
require_once('facebook/client/facebook.php');
/* initialize the facebook API with your application API Key
and Secret */
$facebook = new Facebook(YOUR_API_KEY,YOUR_SECRET_CODE);
/* require the user to be logged into Facebook before
using the application. If they are not logged in they
will first be directed to a Facebook login page and then
back to the application's page. require_login() returns
the user's unique ID which we will store in fb_user */
$fb_user = $facebook->require_login();
/* now we will say:
Hello USER_NAME! Welcome to my first application! */
?>
Hello <fb:name uid='<?php echo $fb_user; ?>' useyou='false' possessive='true' />! Welcome to my first application!
<?php
/* We'll also echo some information that will
help us see what's going on with the Facebook API: */
echo "<pre>Debug:" . print_r($facebook,true) . "</pre>";
?>
So what happens when a user hits the Canvas Page URL (from the example it would be: http://apps.facebook.com/makebeta/)? The require_login() call will produce a screen like this for the user:
If you change require_login() to require_add() the user will get a page that looks like this:
After the user logs into or adds the application they will get the canvas page with the “Hello…” text. It should look something like this:
Facebook Markup Language - FBML
Facebook has provided a bunch of built in tags that will render dynamic data inside your application. All you have to do is include the tags with the correct parameters. In the example above the fb:name tag is used to generate the user’s name on the canvas page. fb:name has a couple of parameters, one of which is uid. In the example above we set uid = $fb_user which is the unique ID of the current user. There are lots of FBML tags you can use, check them all out at: developers.facebook.com/documentation.php?doc=fbml
Facebook API REST-based Interface
The Facebook Client Library provides you with an easy to use wrapper for the Facebook API REST interface. All of the API calls are available under the $facebook->api_client object (after you initiate the $facebook object). Many of the calls will require that the user has either added or logged into the application. Here’s an example call that would retrieve the user’s About Me text from their profile:
$fb_user = $facebook->user;
$about = $facebook->api_client->users_getInfo($fb_user,'about_me');
There are a number of API calls, and a list of them can be found here:
developers.facebook.com/documentation.php
Resources and Further Reading
These are very helpful pieces of information that I highly recommend reading before you get too far into making your Facebook application.
- Basic Facebook Application Architecture
- How URLs Are Handled Within An Application
- Understanding Your Callback Page
And of course the official documentation / developer site:
Other Facebook tutorials and how-to articles:








83 responses so far ↓
1 How To Build A Facebook Application With PHP // Jul 31, 2007 at 4:51 pm
[...] Learn how to build your own Facebook applications using PHP. 6 simple steps to get you started.read more - Facebook PHP Tutorial | digg [...]
2 developercast.com » MakeBeta Blog: Facebook PHP Tutorial // Aug 1, 2007 at 9:28 am
[...] Laing from the MerchantOS group submitted a link to a Facebook tutorial that shows hos to get connected to one of the most popular social networking applications via PHP. [...]
3 Facebook API Tutorial for PHP Developers « FlashColony webmaster’s blog // Aug 3, 2007 at 5:56 pm
[...] To read the tutorial click here. [...]
4 links for 2007-08-07 at Chris Dalby Untangles Networks // Aug 7, 2007 at 4:35 pm
[...] Facebook PHP Tutorial echo “ [...]
5 Enlaces interesantes: botón derecho en flash,crear un irc-bot en php, crear una aplicación para facebook en php, dominando ajax // Aug 31, 2007 at 5:30 am
[...] Cómo crear una aplicación para FaceBook en php [Makebeta] [...]
6 mcdf // Sep 2, 2007 at 5:55 am
Learn how to build your own Facebook applications using PHP. 6 simple steps to get you started.read more - Facebook PHP Tutorial | digg
7 seviyorum seni // Sep 2, 2007 at 5:55 am
good tutorial man ..
8 Gp // Sep 22, 2007 at 4:49 am
Thanks nice beginners tutorial, i have a question.
After writing PHP code where do you host them, if you host it on servers other than on facebook where do you provide URL for this server/page?
9 justin // Sep 22, 2007 at 3:57 pm
Gp,
Facebook doesn’t host your code at all. They simply proxy requests through their servers to yours.
You can host your code anywhere you want. Even on your home computer (though I wouldn’t recommend it).
Take a look here for how to setup your application within facebook:
http://www.ajaxninja.com/?p=55
That articles details all the fields including the ones where you put in the URL of your servers that facebook will send requests to.
The particular field you want to look at is called “Callback Url”.
-Justin
10 Mark // Oct 11, 2007 at 6:41 am
Great Info; thanks for putting this together! The following blog has a small example showing Ajax integration:
http://facebookworld.blogspot.com/
11 Syerwin // Nov 5, 2007 at 5:31 am
hi i canot retrieve or access the data from array that given by users_getInfo function.
Can give me an example plizz..
thanks
12 Bill Dawson // Nov 9, 2007 at 3:29 am
email and website are real, but this question has to do with Facebook: I have a task that is initiated on my canvas page that takes longer than FB’s 8 second timeout.FBJS drives me nuts! How can I issue this in an AJAX-kind-of-way that kicks off the request and returns control back to FB - and then displays a result upon success (or failure)? The routine compares info about the user’s friends. Large numbers of friends tend to exceed the built-in timeout.
13 justin // Nov 9, 2007 at 10:38 am
Bill,
You’ve ventured beyond my knowledge of Facebook. Sorry I can’t help.
14 Adam // Nov 12, 2007 at 8:45 pm
An excellent tutorial. Very informative. One thing I will say regarding the mentioning of AJAX, is that I’ve recently been getting into JSON and the 2 work - AJAX and JSON, work perfectly hand-in-hand. Furthermore, I’ve been dealing a lot of with Facebook recently and so this guide will prove invaluable.
15 Chris Leeman // Nov 21, 2007 at 10:25 am
You spelled ’secret’ wrong in the source code (YOUR_SECRETE_CODE)…
16 justin // Nov 21, 2007 at 10:43 am
Chris, thanks, fixed it =)
17 Heba Nahas // Nov 26, 2007 at 8:14 am
Hii .. I am from syria
And we have a problem.. The facebook is blocked …
I don’t know what should I do to fix this matter..
Is there any help for me
18 justin // Nov 26, 2007 at 9:46 am
Try using a proxy?
19 Mazeed // Nov 28, 2007 at 12:18 am
good site
20 Pam // Nov 29, 2007 at 11:07 pm
Thanks for this tutorial!
I am hosting my app at a Linux server running PHP 4.
May I ask help… I have been having problems with require_login and require_add… Even if I am logged in, I am prompted for my password. Then, when I enter my password I am redirected to my home page rather than to the canvas of the application. Same thing with require_add… it does not bring me to the add application page… it redirects me to my home page.
What did I do wrong?
21 justin // Nov 29, 2007 at 11:13 pm
Pam, I never tried it with PHP4 so I’m not sure if that’s the problem. To me it sounds like you did something wrong when you registered the app with facebook.
Check out this post that I linked to that walks through the application setup form and see if you can find some mistakes:
http://www.ajaxninja.com/?p=55
Good luck!
22 nirzar // Dec 15, 2007 at 4:40 am
Giving me following error
Parse error: syntax error, unexpected T_STRING in /www/app/index.php on line 8
23 Great Facebook App Building Tutorial « TechFold // Dec 16, 2007 at 9:39 pm
[...] a great Facebook Application building tutorial that walks the reader through the entire “Hello world” process, including [...]
24 links for 2007-12-17 « AB’s reflections // Dec 17, 2007 at 1:22 pm
[...] Facebook PHP Tutorial how to write Facebook applications with PHP (tags: api facebook php tutorial development) [...]
25 links for 2007-12-18 — SOJo: Student of Online Journalism by Megan Taylor // Dec 18, 2007 at 12:26 am
[...] Facebook PHP Tutorial (tags: facebook php tutorial) [...]
26 Facebook için PHP ile uygulama geliþtirme - Sayfa 2 - Webmaster Forumu // Dec 24, 2007 at 12:24 am
[...] , bu ve burasý belki yardýmcý olur __________________ Errorla Mücadele [...]
27 yarar web design // Dec 27, 2007 at 7:51 am
good tutorial. good document
thanks
28 seo pune // Jan 8, 2008 at 4:56 am
Hey, Thanks was just trying tofigure out how to build my on application.
Nice starting point
29 John Cleary // Jan 14, 2008 at 3:42 am
I think the error nirzar is getting is because of PHP4, not 5 installed. Gonna test now.
30 about UID // Jan 25, 2008 at 1:16 pm
what is the data type of “$fb_user = $facebook->require_login();
“? is $fb_user integer or chars or array? If it is not an integer, please show me how to get the integer UID in facebook? if it is an array, how do I use it in my facebook apps? for example I have a table structure like: ID, facebook_id, date and etc. thanks for you help.
31 justin // Jan 25, 2008 at 2:14 pm
about UID,
I think fb_user is a number. It’s been a while since I was doing this stuff, but that’s what I remember.
32 Shani Khan // Feb 6, 2008 at 6:15 am
I really appreciate your effort and i make my facebook application with the help of your document. One more thing i need to ask, i want to use javascript in my application can you plz help me.
Problem:
I want to hide and show a dive using javascript.
and i want to make and ajax request to update my record in database
33 Tools 4 Facebook News » Archive » A Facebook PHP Tutorial by Makebeta // Feb 9, 2008 at 12:02 pm
[...] will find on Makebeta a nice tutorial that give you the first steps to develop an application on [...]
34 Max // Mar 12, 2008 at 6:44 pm
Parse error: syntax error, unexpected T_STRING in /home2/bmaxguy/public_html/index.php on line 8
Anyone help please ? I’m sure i have php5
35 JK // Mar 21, 2008 at 1:29 pm
Hi,
I am not able to display the user name when i use this statement
Hello <fb:name uid=” useyou=’false’ possessive=’true’ />! Welcome to my first application!
It does retrieve the user id though. Any suggestions?
36 Alvin Chin // Apr 10, 2008 at 10:27 am
I’m trying your tutorial but I keep getting into this problem when accessing my facebook application URL:
Error while loading page from Application
The URL url returned HTTP code 200 and no data.
There are still a few kinks Facebook and the makers of Test FB application are trying to iron out. We appreciate your patience as we try to fix these issues. Your problem has been logged - if it persists, please come back in a few days. Thanks!
I’ve removed the actual http and application information, but you kind of get the idea. I’ve put the Facebook platform library in /usr/local/lib/facebookphp and included that in the /etc/php.ini file, but I’m not sure why I still get this.
37 Ian B // Apr 14, 2008 at 9:58 am
A last a quick and easy demo to get you started in PHP! Thanks!
38 Make Money With Facebook | Make Money Online - Learn the secret to making money online. // May 12, 2008 at 6:35 am
[...] http://www.merchantos.com/makebeta/facebook/facebook-php-tutorial/ - This website has links to other websites that touch on php programming. [...]
39 Building Facebook apps? Good luck… « 57 Channels // May 18, 2008 at 2:30 pm
[...] Before you do anything, join Facebook and get the Developer app on your profile. Make a new application and fill out the minimum of forms. This will get you an application key and a secret, which you’ll need to do anything. This is also where you can get sample code and link your external program to Facebook once you’ve got something to test. The apps usually won’t work outside Facebook. You can also set up a test profile (follow the guidelines) and find information on the Developers Wiki. Here’s a page that goes into more detail. [...]
40 TRIVUz // May 25, 2008 at 11:00 am
great starting.. i was looking for a article like this one.. many thanks.
41 Facebook Application Development | Digital Advantage Electronics // Jun 10, 2008 at 4:08 pm
[...] http://www.merchantos.com/makebeta/facebook/facebook-php-tutorial/ Get Social, Bookmark Us!!: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]
42 Shikhar // Jun 16, 2008 at 11:32 pm
Hi…, superb article.. i was totally confused when i started… this article gave me an idea where to start… Although i have a problem…. i did as u said… but it gives the following error… can u help me out..
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /home/www/shikhartandon.100webspace.net/trialappli/facebook/client/facebook.php on line 38
Thanks in advance…
43 ????? ??????? ?? ???????? ?????? » ITbananas // Jun 18, 2008 at 9:08 pm
[...] ????? ? -php ??? ????? ??????? ????? ????? php ??? ????? rss ???? [...]
44 Ritesh // Jun 20, 2008 at 12:02 am
Hi,
I am created a application. I called getFBML for a user and setFBML with the a new Picture. The Picture is displayed on the users profiles who added my application but no picture is displayed who not added my application.
Because, i am creating a drawing tool, i wanted that any drawing can be posted to any friends profile.
Kindly help.
Ritesh
45 10 Great Facebook Developer Resources | Webmasters by Design // Jul 22, 2008 at 4:25 am
[...] Facebook PHP Tutorial - This is a great tutorial for beginners. It goes through the very basics of what is needed to begin developing with the Facebook APIs and PHP. It Includes a basic application example. [...]
46 dhirendra // Jul 30, 2008 at 4:04 am
plz help in notification email.
same time send notification email to one or more than friends…
47 fabiana // Aug 17, 2008 at 2:00 pm
lo nesesito
48 Willard Interactive // Aug 30, 2008 at 8:35 pm
For those of you getting the following error:
Parse error: syntax error, unexpected T_STRING in /home2/bmaxguy/public_html/index.php on line 8
Try this, it worked for me:
$api_key = “Your Key”;
$secret = “Your Secret”;
$facebook = new Facebook($api_key, $secret);
49 jun // Sep 9, 2008 at 8:29 am
php4 is not object oriented, and will not work.
50 Facebook Application Development // Sep 22, 2008 at 3:12 am
Nice and informative post about facebook application.
51 links for 2008-09-22 « Andy’s Blog // Sep 22, 2008 at 11:32 am
[...] Facebook PHP Tutorial (tags: php facebook) [...]
52 Firesh // Oct 2, 2008 at 11:50 pm
Whr can i get client library for facebook everytime i download from the link provided it comes out empty .. btw i am using windows platform.. but my server tat is hosting it is unix..
53 Code My Media » Hello // Oct 25, 2008 at 3:31 am
[...] http://www.merchantos.com/makebeta/facebook/facebook-php-tutorial/ Written by admin in: generic | Tags: api, facebook, php [...]
54 Tramadol_sciesodiava // Oct 26, 2008 at 7:03 am
Your Web Site is really wonderful and I bookmarked it. Thank your for the hard work you must have put in to create this wonderful facility. Keep up the excellent work!
55 » there are two very good facebook develop … Doing & Done // Oct 29, 2008 at 9:34 am
[...] Facebooker Tutorial is a facebook apps used for facebook development tutorial. other one is Facebook PHP Tutorial, it is for php programming with facebook api. [...]
56 BeerBelly // Oct 30, 2008 at 1:48 pm
The old “clients” folder mentioned in the tutorial had these files in them:
facebook.php, facebook_desktop.php, and facebookapi_php5_restlib.php
The latest facebook-platform.tar.gz file contains a folder now called PHP with those exact same files. So those following this tutorial, just keep that in mind. Reference the PHP folder instead.
Thanks for the tutorial!
57 Nicklaus Deyring // Nov 3, 2008 at 3:31 pm
Hey Justin, great article
58 Jordan G // Nov 13, 2008 at 3:59 pm
Great article - also, for those having trouble with the “unexpected T_STRING” error, you are likely not passing the keys as strings. Try putting single quotes around each one
new Facebook (’API KEY’, ‘SECRET KEY’);
59 Nuwan // Dec 7, 2008 at 6:10 am
Hey nice tutorial,
Thank you for posting.
60 Zuwuhjuj // Dec 13, 2008 at 10:27 am
Thanks!,
61 carlos // Dec 18, 2008 at 2:40 pm
pues quiciera volver habrir mi facebook
62 Harmin // Jan 17, 2009 at 9:36 pm
Can I change my background profile like at Friendster?
63 Links for 2008-09-22 // Feb 7, 2009 at 2:33 am
[...] Facebook PHP Tutorial (tags: php facebook) [...]
64 Robin Aletras // Feb 7, 2009 at 8:34 pm
I am getting an error that was mentioned above but I didn’t see any answer in the posts.
returned HTTP code 200 and no data.
I have tried for hours to get the sample application to work without any modifications other than the settings the instructions give you. My database is getting data. I can see that in PHPmyAdmin. I can also add a tab but get this error:
FBML Error (line 4): illegal tag “body” under “fb:tab-position”
I want to build an app but don’t think I can if I can’t even get the sample to work.
Please help.
65 jverdeyen // Feb 10, 2009 at 3:20 pm
Hi
Is it normal that the following command takes at least 8sec to load :
$facebook = new Facebook(YOUR_API_KEY,YOUR_SECRET_CODE);
It makes my website load so slow… Is there anything I can do about that?
thanks in advance
66 Andres // Feb 19, 2009 at 4:50 pm
Hi.
great tutoria. I was wondering, how can i logout from the application? is there any url? or something?
67 nerea estibaliz // Feb 28, 2009 at 5:08 pm
sepuede estar en varias web a la vez
68 David // Mar 9, 2009 at 6:58 am
please, can i get an easy way to develop an application on facebook. I do not have any prior
php knowledge. So i am asking if i can get a very easy manual or tutorial on how to develop apllications on facebook. Thanks
69 Risorse per creare un’applicazione Facebook in PHP | BigThink // Mar 11, 2009 at 6:24 am
[...] Facebook PHP Tutorial Creiamo il nostro primo “Hello World”. [...]
70 lakshan // Mar 29, 2009 at 7:02 am
good turorial , thank you for posting
71 Morgana // Mar 29, 2009 at 2:07 pm
I’m wanting to make an app similar to pieces of flair, where the users can collect the images in an organizable area, send them to friends, and make their own to add to the main downloads– can anyone help me do this? This app is the only one I’ve seen like it, so I have no clue where to start.
72 Erpasoleh // Mar 30, 2009 at 12:31 am
Nice post, sir
Give me more tips for my projects
73 Ana Rejon // Apr 6, 2009 at 9:38 am
I want cancel my application and I do’nt how could you please help me? I can fun the way for that I use all the windows to explaint my problem but no one can help me I suppose can you please?
74 Bella // Apr 7, 2009 at 2:43 am
hi.. i would like to ask what is the problem of my code regarding this error:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /nfs/c03/h01/mnt/48516/domains/test.com/html/bella/MY_DIR/facebook.php on line 40
pls help….pls…
75 GNC-2007-12-18 #326 | Geek News Central // Apr 16, 2009 at 6:30 pm
[...] Windows Genuine Advantage making some look like Theives Vista makes biggest disappointment List Facebook PHP Tutorial Good Web design Surviving the 2008 Recession Bloglines new Features Google Knol? ISS Spacewalk [...]
76 Imran // May 2, 2009 at 4:31 am
Thanx for the article, quiet helpful. Better than facebook’s ‘getting started’
77 Aminul // May 7, 2009 at 8:18 am
excellent tutorial!! but it’s deal only one page. how to create more pages and linking. Canvas Callback URL= myserver.com/module/index.php. say i have one page invite.php at my server. how shall i make link with index.php and invite.php ?
78 mytwm // May 15, 2009 at 4:10 pm
Excelent tutorial, Thancks
79 harry // May 23, 2009 at 8:56 pm
Now I know how to make facebook apps because of you.. very helpful:)
thanks alot for this great tutorial..
80 Kreativitätstechniken am Beispiel von Widgets // May 29, 2009 at 2:10 pm
[...] Facebook Application Tutorial [...]
81 candra // Jun 13, 2009 at 7:12 pm
good tutorial. I love it
82 Facebook Applications // Jun 15, 2009 at 3:31 am
really helpful Facebook applicaitons PHP Tutorial
83 Sohbet Odas? // Jun 20, 2009 at 1:45 pm
Excelent tutorial, Thancks
Leave a Comment