Facebook API – Part 3

In the third part of my article series about Facebook API I show you how to use server side API functions. For this there are many SDKs available. For this article I use the PHP SDK. The other SDKs don’t really differ in the functions.

Signed request

A signed request is a POST variable that comes from Facebook, if a user has logged in to the app. So you can use the signed request variable to determine that the user is logged in and we can get data from Facebook. To detect a signed request, store it into session and get some user data you can use the following code:

From the time on we get a signed request, we can get data from Facebook. As an example we check if the user is a fan of the page. Additionally we request the Facebook id from the user. The code uses a function called parse_signed_request. This function returns an array with all information from the user. As param you need the signed request and the Facebook security code which can be found at the app site on Facebook (you can also find the app id there). This function is from an external source, so I post it with this in mind:

Facebook or not Facebook?

In my first article I showed you that you communicate with your web application through the Facebook proxy. But you can also communicate with your application directly. What happens? The web application will be displayed without the Facebook frame. If you want to stop that you can check if a user communicates through the proxy. If not, you can make a redirection to the Facebook app. Here is the code:

First you check if the user agent has the string ‘facebook’. If a user communicates over the proxy the there is that sting. If we do not find it, we make a redirection to the Facebook app url.

Mobile or desktop?

Facebook is also used by mobile gadgets, so we also have to deal with mobile devices. The problem: a Facebook app with a Facebook frame is not a good idea for mobile devices. It  is much better to use the whole 810 pixel width for the display on your phone or tablet. For this we need two things:

  1. we have to detect mobile devices from code
  2. mobile devices should display the app without the Facebook frame

For the mobile device detection we need the user agent we already used:

With the isMobileBrowser function we check if the user agent contains strings that are used by mobile devices. The function looks as follows:

Now we know if we have to display the data for desktop or mobile devices. The only thing is now to redirect to the correct target. This can be Facebook for desktop devices or the web application for mobile devices:

Part 1 | Part 2 | Part 4

(Visited 296 times, 1 visits today)

Leave a Reply

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