Makebeta

Web Developer Tools and Tutorials

Facebook PHP Tutorial

July 31st, 2007 · 38 Comments

Facebook Platform Intro

Facebook Is The New Black.

Everyone is talking about Facebook and their new Application Platform:

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:

  1. Installing The Facebook Developer Application
  2. Downloading The Facebook PHP Client Library
  3. Creating Your Application
  4. Hello Facebook! Example
  5. FBML - Facebook Markup Language
  6. Using the Facebook API

F8 Logo

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:

Facebook Developer Icon 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 Developer Set Up New Application

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

Facebook App Key

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:

Facebook Require Login Screen

If you change require_login() to require_add() the user will get a page that looks like this:

Facebook Require Add Screen

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 Hello Screen

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.

And of course the official documentation / developer site:

Other Facebook tutorials and how-to articles:

Tags: facebook

Makebeta is community resource produced by MerchantOS. MerchantOS is Point of Sale Software that makes running your retail business easy by organizing your sales and inventory. There's no software to install and data backups are automatic. Get your point of sale and inventory control the easy way with MerchantOS.

38 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.  […]

Leave a Comment