Fetch facebook fan pages liked by your friend using php and fql

by  
Share:
facebook

In my last facebook tutorial you guys have seen how to fetch your friends and their profile picture using php and fql, so extending it a bit further this time I am going to demonstrate how to fetch facebook fan pages liked by your friend using php and fql. As per my research I haven’t seen facebook fan page integration in most of the app but you never know when it comes handy.

What is Facebook fan page?

Facebook gives users a facility where they can showcase their business ( NGO’s, schools, colleges, movies etc..) related news/products/events etc.. and can directly interact with other users and that also for free. Right now almost every single organization are in facebook.

Permissions required for fetching facebook fanpages

Unlike my last tutorial where you can fetch friends profile details without any permission, you have to use permission this time in order to fetch facebook fanpages. To fetch the facebook fanpage liked by your friends and you you will have to use user_likes,friends_likes permissions.

define("PERMISSIONS", "user_likes,friends_likes");

Fetch all your facebook friends in a dropdown list

In order to fetch your friends facebook fanpages, first you have to fetch them. So first of all you fetch your friends name and id using php and fql, and then add them in a dropdown list. Add that dropdown list in a form and on form submit event fetch their respective facebook fanpages.

<?php
try {

    $friendsFQL    =   "SELECT uid, name FROM user WHERE uid IN ( SELECT uid2 FROM friend WHERE uid1 = me() ) ORDER BY name ";
    $param  =   array(
		'method'    => 'fql.query',
		'query'     => $friendsFQL,
		'callback'  => ''
	);

    $friendsResults = $facebook->api($param);

  } catch (FacebookApiException $e) {
	print_r($e);
  }
?>

Add those results in a dropdown and bind it to a form.
<form method="get" action="home.php" name="f">
<select id="select" class="form-control" name="friends_id">
  <option value="me">Me</option>
  <?php
  for($i=0; $i<count($friendsResults); $i++) {
    $sel = ($friendsResults[$i]["uid"] == $_GET['friends_id']) ? ' selected ' : "";
    echo '<option value="'.stripslashes($friendsResults[$i]["uid"]).'" '.$sel.' >'. stripslashes($friendsResults[$i]["name"]).'</option>';
  }
?>
</select>
<button class="btn btn-primary" type="submit">Submit</button> 
</form>

FQL for fetching facebook fanpages

This is the main hero of our script. Just make sure you give the correct  user id and you will get proper results.

$user_id = ($_GET['friends_id'] == "" || $_GET['friends_id'] == "me" ) ? "me()" : $_GET['friends_id'];

try {
$fql  =   "SELECT name, fan_count, pic_big, page_url FROM page WHERE page_id IN (SELECT page_id FROM page_fan WHERE uid=$user_id "
      . "ORDER by created_time DESC) ";

$param  =   array(
  'method'    => 'fql.query',
  'query'     => $fql,
  'callback'  => ''
);

$fanPages =   $facebook->api($param);

} catch (FacebookApiException $e) {
  print_r($e);
}

Display the facebook fanpages listing

Now that you have done all the hard work just display the facebook fanpages listing that you have retrieved using fql query earlier.

<?php
  for($i=0; $i<count($fanPages); $i++) {
?>
<div class="col-lg-3 adjustImage">
  <img src="<?php echo stripslashes($fanPages[$i]["pic_big"]); ?>" class="img-circle" width="200px" height="200px">
  <div class="textContainer caption" >
    <?php echo stripslashes($fanPages[$i]["name"]); ?>
    <br>
    <strong>Fan Count:</strong> <?php echo stripslashes($fanPages[$i]["fan_count"]); ?>
    <br>
    <a class="btn btn-primary btn-sm" href="<?php echo stripslashes($fanPages[$i]["page_url"]); ?>" target="_blank"  >View Page</a>
  </div>                  
</div>
<?php
}
?>
[download-file]

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

s.shivasurya on

nice post bro!

Reply
srinuchilukuri on

Hi Nice Article..
I’m downloaded code and run in my local machine but i’m getting errors..
when i click the sign in with facebook button it ask fb credentials after that display a pop up with following message “will receive the following info: your public profile and likes” accept to click “okay” button then it will redirect to my local working file and display errors like “Facebook api Exception object Error code 601”.

please find the following attachments:
1) fb popup.
2)Errors screenshoot.

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.