If you've been keeping up with the latest series of posts on building Facebook applications, you know that we've added our application into Facebook, and then we built a basic application using Javascript. This does pretty good when we can control everything client-side, but what happens when we need to do something on the server?
Facebook (currently) gives us four different SDKs: Javascript, iOS, Android, and PHP. Today we're going to dig into the PHP SDK and build a basic server-side application.
Unlike the Javascript SDK, the PHP SDK requires you to download and install the files onto your server. The files are available on Github at https://github.com/facebook/php-sdk. Head over there and download the latest release.
When you expand it you'll get a few different folders. We're interested in the src
directory as it contains the actual SDK. There are two files here that contain code, the base_facebook.php
and the facebook.php
files. The former file contains a lot of base things that the latter file takes advantage of. For the sake of convenience, you can include just facebook.php
in your code, and it will pull in the base file itself.
Secrets to Getting Started
Like the Javascript SDK, you'll need to get the App ID as well as the App Secret. Both of these are used by the SDK to sign requests when you try to access information from Facebook. To get going, we'll need to make a new Facebook object:
require_once 'facebook.php';
$fbConn = new Facebook(array('appId' => MY_APP_ID, 'secret' => MY_APP_SECRET, 'cookie' => true));
This is our entry point into Facebook. From here we can access the Graph API, allow the user to log in and out, and get our access token. The first thing we need to do though is get permission from the user to access their information.
Getting Permission
We need to allow the user to give our application permission. We can do this by presenting the user a login link that will push them to Facebook, check permission, and return them to our site.
$loginOptions = array(
'scope' => 'email,user_about_me,publish_stream',
'cancel_url' => 'http://myapp.com/fb_cancel',
'redirect_uri' => 'http://myapp.com/fb_login',
);
$user = $fbConn->getUser();
if(!$user) {
echo '<a href=""'.$fbConn-">getLoginUrl($loginOptions).'">Log In</a>';
} else {
echo '<a href=""'.$fbConn-">getLogoutUrl(array('next' =>; 'http://myapp.com')).'">Log Out</a>';
}
$loginOptions
is an array that tells Facebook what to do during login, as well as what permissions we want. There is a long list of what you can ask for. We also give it two URLs, which will fire off depending if the user cancels the permission request or accepts it. If the user is logged in and has given us permission, we'll give them a logout link. This will log them out of Facebook.
Getting Information through the Graph API
The user object from above really doesn't do much. To work with the user and get information, or post information, we need to interact with the Graph API. Just like in the Javascript API, our Facebook connection object $fbConn
exposes an api()
method. We can use this function to query the Graph API to work with the user. For example, if we wanted to get the basic information for the user, we can query the '/me' path:
$userData = $fbConn->api('/me');
You will get back an array populated with common data, like their user ID, nickname, first name, last name, etc. The Graph API Reference gives a full list of the types of resources that are exposed via the Graph API. Just like in Javascript, if we want to post to the user's stream, we use the Graph API:
$fbConn->api('/me/feed', 'POST', array(
'access_token' => $fbConn->getAccessToken(),
'message' => 'This is being posted from my website!',
'name' => 'My Website',
'link' => 'http://myapp.com',
'description' => 'A fake sample app',
));
One thing to notice with this request is the access_token
option. Many of the actions that deal with sensative information or acting on behalf of the user will require an access token to be sent. This is part of the connection object so it is easy to get to. Just check the documentation for the Graph API resource you want to use.
From Here
This gives a pretty basic overview of interacting with Facebook from the server side of things. Depending on how complicated your application is will determine if you need to use the PHP SDK or the Javascript SDK. If you want deep integration with an existing site, the PHP SDK is a great way to get information from Facebook. Facebook has done a great job of making it easy to switch between the two SDKs, and allowing you to mix and match. For example, you can log the user into Facebook via the PHP SDK, but then use the access token in your Javascript so that the user doesn't need to authenticate twice.
Start exploring the Graph API and you can start adding Facebook functionality to your site.