In our last discussion I was discussed about how to login with facebook in our apps using php. Today I am going to discuss about how to get various information from facebook using fql query.
Before getting information I am going to check login with php is complete or not. If login is complete then I use fql query for getting information from facebook.
In this tutorial I am going to show how to get users basic information, users friend information and users photo from facebook using fql query.
Sample Code:
<?php
require ‘fbconfig.php’; // include fbconfig.php file
if($user){
//>>>>Query for getting user id, name, sex, pic_big from user.
$result = $faceboook -> api(array(
‘method’ => ‘fql.query’,
‘query’ => ‘SELECT uid,name,sex,pic_big FROM user WHERE uid=”‘.$user.'”‘,
));
//>>>>Query for getting user id, first name, square picture of users friend.
$data = $faceboook -> api(array(
‘method’ => ‘fql.query’,
‘query’ => ‘SELECT uid, username, first_name,pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me() ) ORDER BY rand()’,
));
//>>>>Query for getting album id of user.
$album = $faceboook -> api(array(
‘method’ => ‘fql.query’,
‘query’ => ‘SELECT aid FROM album WHERE owner = “‘.$user.'”‘,
));
//>>>>Query for getting big picture source, created time of User’s photo
$photo = $faceboook -> api(array(
‘method’ => ‘fql.query’,
‘query’ => ‘SELECT src_big,created FROM photo WHERE owner = “‘.$user.'”‘,
));
$photos = array();
for($i=0; $i<3; $i++){
$photos[] = $photo[$i][“src_big”];
}
$count = count($photos);
for($i=0; $i<$count; $i++){
$photo[$i] = $photos[$i];
}
$friend1 = $data[0][“first_name”];
$friend2 = $data[1][“first_name”];
$user_name = $result[0][“name”];
$image = $result[0][“pic_big”];
$user_id = $result[0][“uid”];
}
else{
echo ‘false’;
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>FQL</title>
<link rel=”stylesheet” type=”text/css” href=”style.css” />
</head>
<body class=”wrap”>
<?php
if(empty($user)){
?>
<div>
<div class=”body-left”>
<a href=”<?php echo $loginUrl; ?>”>Login with Facebook</a>
</div>
<div>
<div style=”background-image:url(ph.jpg)”>
</div>
<div style=”background-image:url(ph.jpg)”>
</div>
<div style=”background-image:url(ph.jpg)”>
</div>
</div>
</div>
<?php
}
else{
?>
<div id=”fb-root”></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = “//connect.facebook.net/en_US/all.js#xfbml=1&appId=155380811335739″;
fjs.parentNode.insertBefore(js, fjs);
}(document, ‘script’, ‘facebook-jssdk’));
</script>
<a >Like</a>
<div class=”header”>
<div class=”fb-like” data-href=”http://bksearch.com/fbphp/index1.php” data-width=”450″ data-show-faces=”false” data-send=”false”></div>
</div>
<div>
<div class=”body-left”>
<p>User Id: <?php echo $user_id; ?></p>
<p>Welcome <?php echo $user_name; ?></p> <img src=”<?php echo $image; ?>” width=”250px;” height=”180px;” alt=”Image” />
</div>
<div class=”body-right”>
<h3>Friend List</h3>
<?php
$total_friends = count($data);
echo ‘Total Friends: ‘.$total_friends;
for($i=0; $i<$total_friends; $i++)
{
?>
<div>
Name: <?php echo $data[$i][“username”].'<br>’; ?>
</div>
<?php
}
?>
<div class=”body-right”>
<?php
if($count >=3){
for($i=0; $i<$count; $i++){
?>
<div class=”body-right-inner” style=”background-image:url(<?php echo $photos[$i]; ?>)”></div>
<?php
}
}
else{
for($i=$count; $i<3; $i++)
{
?>
<div class=”body-right-inner” style=”background-image:url(ph.jpg)”></div>
<?php
}
}
?>
</div>
</div>
</div>
<div class=”footer”>
<a href=”https://www.facebook.com/dialog/apprequests?app_id=155380811335739&message=Facebook%20Dialogs%20are%20so%20easy!&redirect_uri=http://bksearch.com/fbphp/index1.php”>Send Request</a>
</div>
<?php
}
?>
</body>
</html>
Output: