Connecting, Getting User Info & User’s Fan Pages From Facebook

It took me hours to track down a bunch of the code examples I needed for the Facebook canvas application I am building. How to connect your canvas application to Facebook and how to pull the user info and a list of fan pages that the user admins. So now that I have all this info I am going to post it here to share with my readers in case you ever need this.

Connecting your application to Facebook

You will need to have the facebook.php file on your server. Download it here: facebook.zip

Add the code below into the head area and you will need to replace the APP ID and the APP Secret which you get when you :


require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOUR_APP_SECRET',
  'cookie' => true,

$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {

if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();


After the opening body tag add this:

<div id="fb-root"></div>
      window.fbAsyncInit = function() {
          appId   : '<?php echo $facebook->getAppId(); ?>',
          session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
          status  : true, // check login status
          cookie  : true, // enable cookies to allow the server to access the session
          xfbml   : true // parse XFBML

        // whenever the user logs in, we refresh the page
        FB.Event.subscribe('auth.login', function() {

      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;

At this point you can tell the user to connect to Facebook and this is done very simply. First you check if they are already connected with an IF statement like this:


If $me is set, means they are already connected and the $session and $me variables are available. However if it is not set then we want to show the connect button like this:

else :
echo ‘Not connected’;
echo ‘‘;

Notice I added the perms=”email” into the button code, this is where you tell facebook what permissions you want the user to allow you to have.

So here is the complete code:


if($me) :

// get user vars

else :
		echo 'Not connected';
		echo '<fb:login-button perms="email"></fb:login-button>';


Getting User Info Variables From Facebook

Once the user is connected, you can get all kinds of info from Facebook. Here are a few examples of how to call this info.

The $session Array

The first info holder is the $session array. It holds the main connection session info.

Here is what’s inside $session:

$session['session_key'] => 3.1qVoJBwHceusdjIHofdgpI2Nig__.2500.12098375460-20938756
$session['uid'] => 29873546023
$session['expires'] => 20897352352
$session['secret'] => oiUGOF78h39h329498hf
$session['base_domain'] => domain.com
$session['access_token'] => realLongString_wedg80273hd2uy3g29h9
$session['sig'] => 6kjbIUH98wh0ed2

The ones you will probably use are $session['uid'] which gives you the user’s Facebook User ID, the other one is $session['access_token'] which you need to access that special permissions items like user’s email, birthdate and others. I will go into that a little later.

The $me Array

The next big array that holds a lot of info for ya is $me array. This is a good one to use for most of the user’s info, I did not use the $session array for much except to call the special permissions info.

Here is what’s inside $me:

$me['id'] => 923846520
$me['name'] => Fname Lname
$me['first_name'] => Fname
$me['last_name'] => Lname
$me['link'] => http://www.facebook.com/username
$me['location'] => Array
[id] => 238746520387598
[name] => City, State

$me['bio'] => User’s bio.
$me['gender'] => male
$me['timezone'] => -8
$me['locale'] => en_US
$me['verified'] => 1
$me['updated_time'] => 2010-11-27T20:10:25+0000

Getting A List Of User’s Pages

Sometime you might have a need to get a list of the user’s fan pages that they are the admin to. To do that you will need to run a curl function. This means you need to make sure your server has CURL installed and enabled.

Here is the CURL function that will get the list of fan pages:


function getallpages($uid,$atoken) {

	$url = "https://graph.facebook.com/".$uid."/accounts?access_token=".$atoken;
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: graph.facebook.com'));
	$output = curl_exec($ch);

	return $output;


$pagesarray = getallpages($me['id'],$session['access_token']);


This function can be used for various different requests… in this case we called for user accounts (that is what they call pages). This API call url: https://graph.facebook.com/USER_ID/accounts?access_token=ACCESS_TOKEN gets you the accounts (pages). You can also use it to call photos, videos, friends and etc. You just replace the word accounts with whatever else you want to call. You can find all the variables here: Graph API Reference – User

Well, that’s about it… Hope someone finds it useful.

Until next time.

~ Valiik

You Might Also Like:

  • Huge List Of 395+ Web Design Resources
  • New Graphic Design Jobs Board Unveiled Recently
  • Vintage Backgrounds
  • Adding Custom HTML Box To My Facebook Page
  • Adding Custom HTML Box To My Facebook Profile

Author's Website