A concise Facebook API / PHP interface
Facebook has a client library for PHP to interact with its API, but I found it very bloated and inefficient with many redundancies. So I set out to make my own. My primary aim was to keep it very simple – with just a few functions to retrieve any data made available by the API in a simple format.
In the end, I came up with a small class. The constructor takes two arguments, the Facebook application’s public API key, and the private key (just like the original library). However, there are only two other functions – facebook::login()
, which is similar to the original library’s FacebookRestClient::require_login()
, but takes no parameters, and facebook::call()
.
login()
is called if you require the client to allow your app access to their user data – if they haven’t, it will redirect to the login page. If they are already authenticated it will do nothing.
call()
takes two parameters – the method, as listed in the Facebook Developers Wiki, and the other arguements you want to pass to Facebook’s REST server, in associative array form. The required arguements, such as the key, do not need to be specified, and the signature is automatically generated. call()
returns a PHP stdClass. So, if you wanted to get the public information for application 2342134, then, after including the class, you can call call()
as such:
$api_key = "c51ce410c124a10e0db5e4b97fc2af39";
$secret = "d3d9446802a44259755d38e6d163e820";
$facebook = new Facebook($api_key, $secret);
$args = array("application_id" => 2342134);
$info = $facebook->call("Application.getPublicInfo", $args);
And everything will be in the $info
object.
The script can be downloaded from the lab at http://lab.aspektas.com/facebook.php. It is released under the Creative Commons Attribution License 3.0, so feel free to use it. And as a footnote, the class isn’t magical, and probably can’t do everything, but for FBML applications it is a viable alternative. I’ll post updates here too.