views:

32

answers:

1

Hi There,

I have 4.5 years experience as a professional PHP developer, I have loads of experience with session handling, etc.

I am wanting to learn how to build Facebook applications. I have gone through the process of downloading the Facebook Developer application, I have used to it Create an App, I have set the canvas URL to where the app is hosted.

I can successfully "install" the app using a Facebook user account, and I can successfully access it. I have noted that when the app is loaded, a whole string of data gets passed to it, with parameter names like fb_sig_user and fb_sig_session_key. Currently, I am doing nothing with these parameters.

On the application end, obviously I am supposed to create a Facebook object by using my own APP ID key and SECRET key, which I have done.

But I can't seem to figure out how to start making API calls from my application back to Facebook.

How do I, at this point, just do something simple like get the person's name and display it to them?

Eg, how do I:

$first_name = $facebook->getUserFirstName();

.. do something like that?

I just want to do a simple test to ensure the API stuff is working.

Any help would be appreciated. Thanks.

+2  A: 

The Facebook PHP SDK would probably be your first stop, with some example code as a close runner up.

Making API calls with the Facebook SDK set up alright is as easy as;

$me = $facebook->api('/me'); // Returns array containing data for currently logged in user depending on given permissions

or

$my_friends = $facebook->api('/me/friends'); // Returns array containing information on friends of currently logged in user

and even

$my_profile_img = $facebook->api('/me/picture'); // Returns a string containing URL to users profile picture.

Also, everything within the open graph has unique id's that you can plug in instead of me in /me.

For further and more in-depth description of the Open Graph, I suggest going to the official Graph API Overview page.

Quite easy and fun to work with once you get it set up - albeit a bit slow.

Update:

In order to get the Facebook cookie and set up a session from it I did:

$cookie = get_facebook_cookie($fbconfig['appid'], $fbconfig['secret']);

function get_facebook_cookie($app_id, $application_secret) {
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
  ksort($args);
  $payload = '';
  foreach ($args as $key => $value) {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
    }
  }
  if (md5($payload . $application_secret) != $args['sig']) {
    return null;
  }
  return $args;
}

if (isset($cookie)) {
$session = $facebook->getSession($cookie);
}

Then updated Facebook.php line 332 to be

public function getSession($passedCookie) {

and line 373 to be

$this->setSession($passedCookie, $write_cookie);

Lame hacking, but it worked for me at least

Freyr
Thank Freyr but it's the set up part I am struggling with. None of my API calls are working. I am using the SDK.
Callum
There must be some way I can use the parameters that Facebook is passing to my application to activate a session and start doing these API calls?
Callum
If $session = $facebook->getSession(); doesn't work for you, you could always try to set it manually using facebook cookie. I've updated my answer with a hack that got it working for me(when using XFMBL login).
Freyr
You could always try to just run the example.php as your canvas app for testing purposes as well.
Freyr