Facebook PHP Tutorial

by justin on July 31, 2007

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:

{ 28 trackbacks }

How To Build A Facebook Application With PHP
July 31, 2007 at 4:51 pm
developercast.com » MakeBeta Blog: Facebook PHP Tutorial
August 1, 2007 at 9:28 am
Facebook API Tutorial for PHP Developers « FlashColony webmaster’s blog
August 3, 2007 at 5:56 pm
links for 2007-08-07 at Chris Dalby Untangles Networks
August 7, 2007 at 4:35 pm
Enlaces interesantes: botón derecho en flash,crear un irc-bot en php, crear una aplicación para facebook en php, dominando ajax
August 31, 2007 at 5:30 am
Great Facebook App Building Tutorial « TechFold
December 16, 2007 at 9:39 pm
links for 2007-12-17 « AB’s reflections
December 17, 2007 at 1:22 pm
links for 2007-12-18 — SOJo: Student of Online Journalism by Megan Taylor
December 18, 2007 at 12:26 am
Facebook için PHP ile uygulama geliþtirme - Sayfa 2 - Webmaster Forumu
December 24, 2007 at 12:24 am
Tools 4 Facebook News » Archive » A Facebook PHP Tutorial by Makebeta
February 9, 2008 at 12:02 pm
Make Money With Facebook | Make Money Online - Learn the secret to making money online.
May 12, 2008 at 6:35 am
Building Facebook apps? Good luck… « 57 Channels
May 18, 2008 at 2:30 pm
Facebook Application Development | Digital Advantage Electronics
June 10, 2008 at 4:08 pm
????? ??????? ?? ???????? ?????? » ITbananas
June 18, 2008 at 9:08 pm
10 Great Facebook Developer Resources | Webmasters by Design
July 22, 2008 at 4:25 am
links for 2008-09-22 « Andy’s Blog
September 22, 2008 at 11:32 am
Code My Media » Hello
October 25, 2008 at 3:31 am
» there are two very good facebook develop … Doing & Done
October 29, 2008 at 9:34 am
Links for 2008-09-22
February 7, 2009 at 2:33 am
Risorse per creare un’applicazione Facebook in PHP | BigThink
March 11, 2009 at 6:24 am
GNC-2007-12-18 #326 | Geek News Central
April 16, 2009 at 6:30 pm
Kreativitätstechniken am Beispiel von Widgets
May 29, 2009 at 2:10 pm
benjamin junior » Blog Archive » Bookmarks em August 18th
August 17, 2009 at 6:12 pm
Facebook’s “Hello, World!” Sample PHP Script « Your Computer Guy in Southwest Florida
November 11, 2009 at 5:03 pm
Ten Ways to Improve Your Facebook Marketing Campaign Today « Mike Gingerich Blog
November 17, 2009 at 7:24 am
Desarrollo de aplicaciones facebook | Blog Desarrollo - Información para desarrolladores
November 17, 2009 at 11:30 am
Programming Networking Project Management Software Engineering Database » Blog Archive » Facebook Application Development Resources
January 14, 2010 at 10:17 pm
Facebook PHP Application Tutorial from Makebeta « Facebook applications for developers
February 1, 2010 at 5:42 am

{ 105 comments… read them below or add one }

Facebook Aplication Developer March 3, 2010 at 9:06 am

Sir
I read your tutorail its nice you good define about facebook aplication for php programmer can do work easily Thankx

Ben Goevaerts March 7, 2010 at 8:34 am

Hi!! Great Tutorial!
Is there a way to get more info out of the user?
Like date of birth and Email adres …

Thanks !

cinsel sohbet March 11, 2010 at 6:23 am

I learned to do it way back in high school with Flash 5, or turn off the music.
It’s really good written and I fully agree with You on main issue, btw. I must say that I really enjoyed reading all of Your posts.

aashree March 11, 2010 at 1:10 pm

im trying the following code but a T_STRING error is being displayed for it..
print(‘text’);

aashree March 11, 2010 at 1:11 pm

im trying the following code to be implemented in a facebook application , but a T_STRING error is being displayed for it..
print(‘text’);

Leave a Comment

Previous post:

Next post: