How I learned to stop hating the Facebook API – Part One

I’m not a PHP programmer.  Let me rephrase that.  I’m not a PHP programmer, on purpose.  It’s not that it’s too difficult for me or anything along those lines, I just decided that there wasn’t a future there for me. Soooooo many other people jumped on the PHP band wagon, I decided not to try to be part of an increasingly over saturated market.  My opinions aside, it’s impossible to do anything web related without at least knowing how to get around PHP, and if you’re wanting to incorporate your game with Facebook there’s really no way around using it.  It’s not a hard language, and it’s pretty intuitive, so using it with the Facebook API should be no problem, right?  Right!?

I’m not going to show you how to do really awesome stuff like publish awards to user’s streams from your game here. I’m just going to get you over a massive hurtle if you find yourself in the spot I was earlier today.  Writing an app that works in Facebook has three big problems.

1. Facebook changed their API so that every google search you do has little to no effect on your problem.

2. Facebook won’t let you submit apps to their Application Directory unless you have a wizard, priest, or mage as a close personal friend

3. Facebook’s Platform documentation is filled with out-dated nonsense and example code filled with deprecated functions you can’t possibly know about if you’re a beginner to their API.

I should go ahead and let you know this now.  I’m writing this on August 5th, 2010.  I can not guarantee any of what I’m about to explain will still be valid in the future since Facebook likes to change things all the freaking time.

PROBLEM ONE:

Let’s start at the beginning.  You must first set up your Application through Facebook.  This has nothing to do with your game itself, it’s just the place you start in Facebook. In the Canvas Callback URL field you’ll put the path to your game.  My game is at http://www.blamingtim.com/test/index.php, so In this field I’ll put http://www.blamingtim.com/test/

Make sure you’ve selected IFrame as your render method and not FBML.  Do yourself a favor now and ignore FBML.  If you come across examples on the internet with lines like this:
<fb:name uid=$user_id useyou=false />
run.  It will do you nothing but harm.

Ok, so what’s the problem?  All I wanted to do was show the user’s pic and name.  Sounds simple enough, right?  Following along with Facebook’s instructions I downloaded the Facebook PHP SDK from github.com and put it into my test directory.  I then needed to create my index.php file to show the user’s picture and name.  Okay, so I create the file and populate it with this:

<?php
// Awesome Facebook Application
//
// Name: UnityTesting
//
require_once 'facebook-php-sdk/src/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId'  => '120343757976612',
'secret' => '837c98a92ec456745a2fedf5213254a',
'cookie' => true,
));
$user_id = $facebook->require_login();
echo "Hello, ";
echo $user_id;
?>

I’m not expecting much here. I’m expecting a blank screen that says: “Hello, 21566890”.  Each Facebook user has a unique user id, and that’s what I wanted to see. I figured I’d start there, and then move onto tricky things later.  Well, this is where everything started to go wrong.  Instead of seeing this, I see a message like this:

Fatal error: Call to undefined method Facebook::require_login()

Uhm… what?  How could this possibly be a Fatal Error?  A quick Google search shows that plenty of people have this problem, but not many of them have a solution posted. The answer is simple.  require_login() isn’t a function anymore.  Facebook changed their API and something a simple as this was taken away.  Ok… so what do I do?  You download the previous version of the API of course!  The trick is that the old and the new API are both called facebook.php.  Why are they both called facebook.php?  Because Facebook is powered by the anger and frustration of new developers, that’s why.

Do yourself a favor, delete the facebook-php-sdk directory you downloaded from Github and replace it with this http://pearhub.org/get/facebook-0.1.0.tgz Facebook calls this the Old Rest API.  It still works, and for Flash and Unity developers it’s much much easier to use. Change your require_once line to the new (or should I say old) facebook.api and re-load your page.

Boom!  Now you should see something like this:

Hello, 2116487620

It may seem simple and stupid, but this took me all morning to track down.  For my next post I’ll show you how to use the Graph API to pull user information, and we’ll talk about Problems Two and Three in greater detail.

Advertisements

0 Responses to “How I learned to stop hating the Facebook API – Part One”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: