Thomas Bolander
Thomas Bolander

Reputation: 3952

Get insights from specific page

I'm trying to get insights out from a specific facebook-page, but it doesn't matter, which user is request the page. but i can't figured it out how to do it.

I've made this code to get the insights with a user-accessToken generated in Graph explorer.

require 'facebook/src/facebook.php';

$facebook = new Facebook(array(
        'appId' => '551080774957462',
        'secret' => 'f821960b551440b80eecd2dc0a0eff2e',
    ));

//AccessToken from Graph explorer
$user_access_token = "CAAH1NH3VeZAYBAGbEZAeMgwwLOxKKmUqtUtbXT6kaO........";

$facebook->setAccessToken($user_access_token);

$facebook->getUser();

//Gets all pages
$user_accounts = $facebook->api("/me/accounts/");

$pageAccessToken = "";

//Find the right page
foreach ($user_accounts["data"] as $page)
    if ("476015619078252" == $page["id"])
        $pageAccessToken = $page["access_token"];

$facebook->setAccessToken($pageAccessToken);

$insights = $facebook->api("/476015619078252_$postID/insights");

The problem with this is that, the AccessToken from Graph explorer allways expired after some hours .

Thanks !

Upvotes: 1

Views: 1714

Answers (1)

Kliptu
Kliptu

Reputation: 199

I think you can use user token instead of page token

<?php
      require 'src/facebook.php';

      $facebook = new Facebook(array(
         'appId'  => 'APP_ID_HERE',
         'secret' => 'SECRET_HERE',
      ));

      $fbID = $facebook->getUser();

      $pages = $facebook->api('/me/accounts');

      $pageID = $pages['data'][0]['id'];

      $insights = $facebook->api('/'.$pageID.'/insights');

      echo '<pre>';
      print_r($insights);
      exit;
?>

You can pass params since to get specific date range result..

Cheers.

Upvotes: 1

Related Questions