Pull pages data via facebook graph

In the last article we discussed how to create or extend access token for facebook pages. Today we are going a bit further and writing a script where we can pull some pages data.
So here is the full code, i will explain it below where necessary.

Save the following code as index.php(or whatever name you like).
[cc lang=”php” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”80″ tab_size=”4″]
<?php
// Facebook APP [Pages App for access tokens]
define(‘APP_ID’, ‘enter_app_id_here’);
define(‘APP_SEC_KEY’, ‘enter_app_secret_here’);

// Access Token
define(‘ACCESS_TOKEN_PAGE1’, ‘enter_access_token_here_for_page1’);
define(‘ACCESS_TOKEN_PAGE2’, ‘enter_access_token_here_for_page2’);

// Page IDs
define(‘APP_PAGE_ID_PAGE1’, ‘enter_page1_id_here’); // for page1
define(‘APP_PAGE_ID_PAGE2’, ‘enter_page2_id_here’); // for page2

// Create array for pages ids, tokens, country etc
$total_pages = array( array( ‘Pageid’ => APP_PAGE_ID_PAGE1, ‘Access_token’ => ACCESS_TOKEN_PAGE1, ‘Country’ => ‘FINLAND’ ), array( ‘Pageid’ => APP_PAGE_ID_PAGE2, ‘Access_token’ => ACCESS_TOKEN_PAGE2, ‘Country’ => ‘SINGAPORE’ ) );

// periods
$day = ‘day’;
$week = ‘week’;

$date = date(“10-07-2013”);

foreach ($total_pages as $pages) {

$pageid = $pages[‘Pageid’];
$accesstoken = $pages[‘Access_token’];
$country = $pages[‘Country’];

// Daily People Talking About This [page_storytellers]
$fql_query_page_storytellers = ‘https://graph.facebook.com/’.’fql?q=SELECT+metric,value+FROM+insights+WHERE+object_id=’.$pageid.’+

AND+metric=”page_storytellers”+AND+end_time=end_time_date(“‘.$date.'”)+

AND+period=period(“‘.$day.'”)&access_token=’.$accesstoken;

$fql_obj_page_storytellers = file_get_contents($fql_query_page_storytellers, true);

//print_r($fql_obj_page_storytellers);
if(!empty($fql_obj_page_storytellers[‘data’])) {
echo $e1 = $fql_obj_page_storytellers[‘data’][‘0’][‘value’];
}
else
{
echo $e1 = “empty string”;
}

// Daily New Likes [page_fan_adds_unique]
$fql_query_page_fan_adds_unique = ‘https://graph.facebook.com/’.’fql?q=SELECT+metric,value+FROM+insights+WHERE+object_id=’.$pageid.’+

AND+metric=”page_fan_adds_unique”+AND+end_time=end_time_date(“‘.$date.'”)+

AND+period=period(“‘.$day.'”)&access_token=’.$accesstoken;

$fql_obj_page_fan_adds_unique = file_get_contents($fql_query_page_fan_adds_unique, true);

//print_r($fql_obj_page_fan_adds_unique);
if(!empty($fql_obj_page_fan_adds_unique[‘data’])) {
echo $e2 = $fql_obj_page_fan_adds_unique[‘data’][‘0’][‘value’];
}
else
{
echo $e2 = “empty string”;
}

}
?>
[/cc]

———————————————————–

PLEASE READ BEFORE USING THE SCRIPT

From the above script change the following vars:

enter_app_id_here = change this to your app id which you created.

enter_app_secret_here = enter the app secret here.

enter_access_token_here_for_page1 = enter the access token for page 1 if you have many pages.

enter_access_token_here_for_page2 = enter the access token for page 2 if you have many pages.

Note: You can add many pages as you want, but if you have only one page, just remove the 2nd array from the $total_pages array for page2. I have added country var to the array, this is only to recognize the data if have many pages while easy to save to database for later use.

enter_page1_id_here = You can get this while following the previous article.

enter_page2_id_here = You can get this while following the previous article.

For the periods, please read the facebook insight page for more details and usage.

There are not much changes required except from the above. But if you want to change the script for your need, go ahead and change/enhance it as it fits your requirements. You can pull more data by reading the insight page OR can save the data to database for later use.

———————————————————–

Open your browser and call index.php. One more thing, facebook does not provide latest data for pages so you have to go back last 3 days, if today is 14-07-2013, you can get the data for 10-07-2013 or maybe 11-07-2013.