Chris Tankersley

  • Home
  • About
  • Portfolio
  • Projects
Twitter RSS

Building Facebook Apps – The PHP SDK

Posted on September 29, 2011 by Chris
No CommentsLeave a comment

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:

1
2
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.

1
2
3
4
5
6
7
8
9
10
11
12
$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=&quot;'.$fbConn->getLoginUrl($loginOptions).'">Log In</a>';
} else {
    echo '<a href=&quot;'.$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:

1
$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:

1
2
3
4
5
6
7
$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.

Categories: PHP, Programming, Tutorials | Tags: Facebook, PHP

About Chris

View all posts by Chris→
Notice: This work is licensed under a BY-NC-SA. Permalink: Building Facebook Apps – The PHP SDK
The MicroPHP Manifesto – What People Are Overlooking
Building Facebook Apps – The JavaScript SDK

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*


question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

  • Search

  • Archives

    • February 2012
    • January 2012
    • September 2011
    • August 2011
    • April 2011
    • December 2010
    • November 2010
    • September 2010
    • August 2010
    • May 2010
    • April 2010
    • February 2010
    • January 2010
    • November 2009
    • October 2009
    • September 2009
    • June 2009
    • May 2009
    • February 2009
    • January 2009
    • November 2008
    • October 2008
    • July 2008
    • May 2008
    • February 2008
    • December 2007
    • October 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
  • Categories

    • Code Releases
    • Doing Development
    • Hardware
    • IBM i
    • JavaScript
    • Joomla!
    • MySQL
    • Non-Programming
    • Operating Systems
    • Personal
    • PHP
    • Programming
    • Project Management
    • Reviews
    • Servers
    • Software
    • This Site
    • Tutorials
    • TWS Software
    • Uncategorized
    • ZendCon 2010
© Chris Tankersley. Proudly Powered by WordPress | Nest Theme by YChong