HOWTO in PHP: Retrieve a list of Facebook Pages you are the Administrator of using Graph API

Here's the code. I am assuming here that you have already logged in as a valid Facebook user with the "manage_pages" scope permission.

<?php

require_once("fbphpsdk/facebook.php");

$config = array();
$config['appId'] = 'YOUR_APP_ID';
$config['secret'] = 'YOUR_APP_SECRET';
$config['fileUpload'] = false; // optional

$facebook = new Facebook($config);

$uid = $facebook->getUser();
$accesstoken = $facebook->getAccessToken();
$params = array('access_token' => $accesstoken);

if ($uid) {
    try {
        $userprofile = $facebook->api('/me','GET');
        echo "Name: " . $userprofile['name'];
        $accounts = $facebook->api('/me/accounts', 'GET', $params);
        foreach($accounts['data'] as $account)
        {
           echo "<hr/>" . $account['name'] . " " . $account['id'] . "<br/>";
        }//end foreach
    }
    catch(FacebookApiException $e) {
        echo "Error accessing Facebook data";
    }   
}
else {
    echo "You are not logged in";    
}
?>

And here's the sample output generated by the code above when I logged in using my Facebook account

Comments

Post a Comment

Popular posts from this blog

How to create an organizational chart in your webpage using Google Organization Chart Tools

Embed JSX code with if condition in React app