facebook score api for game leaderboard – An intoduction.

1,149

April 3, 2012 by admin

Ok lets talk Technical stuff :)

Recently i just completed a promotional campaign game for a big client based in the US. Because of the amount of users and the tight deadline,  I opted for facebook score API to build to manage the scores and boy was it as easy as i thought.

It uses POST or GET to make requests to the open graph api to retrieve and store scores. Read more about the score API here

Normally to create a leaderboad to show users score in relation to friends you have to do this:

1. Get list of all the users friends id
2. Get a list of all users id you have stored that have played i.e database or flatfile
3. Compare the two lists and retrieve the users friends with there score

Sweet yeah…… NO!

Nightmare, all it takes is multiple users with lots of friends accessing the app. The app will have to to a search and compare which can be tidious for the server if you have hundred of thousands of users  in your database. You can soften the blow by increasing the ram on the server , by implementing CDN etc

or

You can use the facebook open graph score API!.

A single call will retrieve all your friends id and will include their score as well for the current app or any particular app.

Once you post a score it also gets published to the wall of the users. You MUST request the publish_actions permission.

Your app must also be under the right category in this case you MUST choose games.

You will need a user User_Token or Oauth_Token to retrieve scores and an App Token to post scores. You can easily get this from the developers section of facebook.

 

The easiest way to get these for testing is to create an app and grab one from the tools section on facebook. Make sure you are logged in with your developer account then go here https://developers.facebook.com/tools/access_token

 

facebook allows you to generate these tokens so you can use it to test, but you may want to look into how to generate one on the fly when your app goes life.

To get started make sure you choose

Now lets do some basic call and post request.

//To retrieve the logged in user’s score, you need the user, access or oauth token.
//we using oauth_token here
$oauth_token =’XXXXXXXXXXXXXXXXXXXXXXXXXXXXX’;
//retrieve the $facebook of current user
$facebook_id =’XXXXXXXXXXXXXXXXXXXX’
$getScores = file_get_contents(‘https://graph.facebook.com/’.$facebook_id.’/scores?access_token=’.$oauth_token);
$myData = json_decode($getScores, true);
$list =’

Name: ‘. $data[0]['user']['name'].’ score: ‘.$data[0]['score'].’

‘;
print($list);

To retrieve user and their friends score for the App you need the user, access or oauth token.

//To retrieve a users post you need the user, access or oauth token.
//we using oauth_token here
$oauth_token =’XXXXXXXXXXXXXXXXXXXXXXXXXXXXX’;
$getScores = file_get_contents(‘https://graph.facebook.com/’.APP_ID.’/scores?access_token=’.$oauth_token);
$myData = json_decode($getScores, true);
$list = ”

    “;

 

    foreach($myData['data'] as $data){

 

    $list +=’

  • Name: ‘. $data['user']['name'].’ score: ‘.$data['score'].”;
    }
    $list += ”

      “;
    print($list);
    [/PHP]

You can post a score or a user by issuing an HTTP POST request to /USER_ID/scores with the app access_token as long as you have the publish_actions permission.

We are using CURL to do the post.

//Get your access and user token as defined above and store it

define(‘ACCESS_TOKEN’, ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’);
define(‘USER_TOKEN’, ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’);

function fbCurlRequest($graphObject, $post = false) {
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, “https://graph.facebook.com/”.$graphObject);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-length: 0′));
if($post == true)
curl_setopt($ch, CURLOPT_POST, true);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data, true);
}

//facebook id of user
$facebook_id = ‘XXXXXXXXXXX’

//total points user earned
$total_points = 20;

//Use the Curl function to post the score
$post_score = fbCurlRequest($facebook_id.”/scores?score=”. $total_points .”&access_token=”.ACCESS_TOKEN, true);

This will return a variable 1 or NULL.


Check a live example here
| Download the source here 


Subscribe

Apps4Pages