jonb's blog.

2/29/08 1:06 PM

Trendrr API example using PHP and Facebook

Here is an example which connects the Trendrr API to the Facebook API, allowing statistical analysis of social data.  The RSVP statuses of a specific event are collected and graphed (i.e. users who have accepted, declined, or are unsure of the invitation).  In return, the Facebook markup language (FBML) is refreshed to show the latest graph for this application on both the user profile and canvas pages.  First, three Custom Data Sets, one for each RSVP status, are created via Start Tracking»Add Custom Data Set on Trendrr.  The API keys provided for each data set will be necessary to submit the number of users in each category.

The Facebook API can only be used within a Facebook application and, therefore, one has been created for this example.  Those new to Facebook development can find out more on the Facebook developer wiki.  The main components of the application will be located within one index.php file described below in detail, one section at a time.

The beginning of this PHP file initializes the typical Facebook information and should look familiar to those who have developed applications for the site.  This piece also establishes a user and session key for a scheduled cron job to update the graph data every hour.  The implementation of a cron job will be discussed later.  For additional information on this technique, visit Infinite Session Howto on the developer wiki site.

require_once 'facebook.php';

$appapikey = '<Your Facebook API key>';
$appsecret = '<Your Facebook secret key>';
$facebook = new Facebook($appapikey, $appsecret);

// Set up user with infinite key (for cron access)
$user = '<user id>';
$key = '<user's session key>';
$facebook->set_user($user, $key);

// Callback url
$appcallbackurl = '<your callback url>';

// Catch the exception that gets thrown if the cookie has an invalid session_key in it
try {
  if (!$facebook->api_client->users_isAppAdded()) {
    $facebook->redirect($facebook->get_add_url());
  }
} catch (Exception $ex) {
  //this will clear cookies for your application and redirect them to a login prompt
  $facebook->set_user(null, null);
  $facebook->redirect($appcallbackurl);
}

Secondly, we need to gather all the appropriate information in order to upload statistics to Trendrr.  Given an event id, the Facebook API is used to return all users that have been invited to this event.  The numbers for each RSVP category can then be calculated and stored.  The last three lines define the API keys for the data sets previously set up on Trendrr.  These keys can be found on your My Graphs»my data page.

// Get event members
$event_id = 8414464684; //Pillow Fight NYC 2008
$event_members = $facebook->api_client->events_getMembers($event_id);

// Get RSVP numbers
$attending = sizeof($event_members['attending']);
$notsure   = sizeof($event_members['unsure']);
$declined  = sizeof($event_members['declined']);

// API keys for Trendrr
$attending_key = '<api key for data set>';
$notsure_key   = '<api key for data set>';
$declined_key  = '<api key for data set>';

The URL syntax defined in the Trendrr API is utilized to retrieve the RSVP data.  The curl functions in PHP allow this page to access these URLs and provide values.

// Send information to Trendrr (if accessed by cron job)
if ($_SERVER['argv'][1] == 'cron') {
    $a = curl_init("http://www.trendrr.com/api/import/timeseries?dataset_key=" . $attending_key . "&value=" . $attending);
    curl_exec($a); curl_close($a);
    $n = curl_init("http://www.trendrr.com/api/import/timeseries?dataset_key=" . $notsure_key . "&value=" . $notsure);
    curl_exec($n); curl_close($n);
    $d = curl_init("http://www.trendrr.com/api/import/timeseries?dataset_key=" . $declined_key . "&value=" . $declined);
    curl_exec($d); curl_close($d);
}

Notice that the curl statements are located within an if statement looking for the argument string 'cron.'  To automatically post data, a cron job has been defined on our UNIX server to access this Facebook application every hour.  Requiring the 'cron' argument for this block of code ensures that data points are not added to Trendrr when the page is accessed manually in a browser.  The line for the cron job is shown below.  We realize that not everyone will have the ability or access to generate their own automated jobs, and therefore we recommend webcron.org, a web service for creating such automated tasks.

0 * * * * php -f /<some directory>/index.php cron

Displaying the latest results is the final step for this Facebook page.  The 'embed_img' URL is the Trendrr defined embedding code on the graph's customization page.  This graph, however, is not one of the above data sets, but rather all three as one.  Within Trendrr, multiple data sets can be combined and then this new graph can be accessed for display.

The embedded image URL will return the latest graph as an image.  Facebook automatically caches application images, thus an API call to refresh the image is necessary due to the graph's dynamic nature.  A Cascading Style Sheet (CSS) is included to display the canvas and profile pages in different styles, but is not necessary.  The FBML is then displayed on the canvas page and passed to 'profile_setFBML' to update the user's profile.

// Refresh the graph image and set the FBML
$embed_img = 'http://www.trendrr.com/public/graphs/369161';
$facebook->api_client->fbml_refreshImgSrc($embed_img);
$fbml = '<img width="380" height="285" src="'.$embed_img.'" />';

// Include style, display FBML for the canvas page, and set the FBML for the profile page
include_once 'lib/main.css';
echo '<div id="canvas">'.$fbml.'</div>';
$facebook->api_client->profile_setFBML('<div id="profile">'.$fbml.'</div>', $user);

Manually visiting this page on Facebook, either on the canvas or profile page, will display the latest results for the graph which combines all three data sets.  When the cron job accesses the page, the curl functions will execute the API defined URLs to send the event information back to Trendrr.

Add Comment:

You must be logged in to comment

TrendrrScratchpad

TrendrrSearch

What is: Trendrr?

Track, compare and share data, free. Identify trends across social graphs and networks, realize the potential of p2p, track engagement metrics, look at what is really happening, real time.

Trendrr: Featured Users

forgot password