Facebook’s API comes wrapped in libraries for all the popular server languages, but there will come a day when you need to debug the raw HTTP transactions that they all boil down to. As a scripting language, the PHP implementation is easy to understand, and I ended up tweaking mine to output the exact text that’s flowing between me and Facebook. This was partly to help debugging, but also for my own curiosity. I’d like to model some of my interfaces on Facebook’s since it’s simple, robust and flexible.
You call a method by sending an HTTP request to "http://api.facebook.com/restserver.php". Arguments to the method are passed in the POST string sent as part of the request. Here’s an example for an event API call, split up on ampersands so that it won’t go off the edge of the blog, and with any secret values replaced with X:
This is generated by taking the normal PHP arguments to each method, along with stored login and API keys, and serializing them into this string. If CURL is present on the server, this is then used to send the request, otherwise PHP’s native HTTP access functions are used.
Assuming that the call name (specified in "method") and the other arguments check out, then the Facebook server will return a string as its response. This string is in XML, and looks something like this:
<?xml version="1.0" encoding="UTF_8"?>
<name>Blog World Expo example</name>
… <snip …
The library then takes this simple XML string, and parses it into a PHP hierarchical array of values that looks like this:
 => Array
[eid] => 5172087276
[name] => Blog World Expo example
[tagline] => http://www.blogworldexpo.com/
[nid] => 0
[pic] => http://profile.ak.facebook.com/object2/5/55/s5172087276_7478.jpg
[pic_big] => http://profile.ak.facebook.com/object2/5/55/n5172087276_7478.jpg
[pic_small] => http://profile.ak.facebook.com/object2/5/55/t5172087276_7478.jpg
[host] => BlogWorld
… <snip> …
This always matches the structure of the XML. Facebook use a restricted subset that avoids tag attributes and anything else that might make it hard to map to this JSON style format.
Another possibility is that an error will be returned. In that case, the XML will normally just be a couple of tags, the error message string and the numeric error code. This gets converted to a PHP exception.
To dig into this code yourself, I recommend looking through facebookapi_php5_restlib.php in the client folder of the Facebook SDK. That’s a good place to add your own debugging code too, though there’s already some that can be enabled by setting the $GLOBALS[‘facebook_config’][‘debug’] variable to true.