Facebook Load User Profile Via Graph API And FQL Query
Posted by in Facebook Graph API September 28, 2011 5 Comments

This post simple display How to read Facebook user profile information (Name, Email, Profile Picture, Locale, Timezone, Total Friends, etc) via PHP Graph API and FQL Query.

First, you need to create a new Facebook application (or use an exiting one) then replace appId and secret with your Facebook app id and app secret in the Initializing section (line 6,7). In this example, I place them in config.php file so you will need to update them in there.

Facebook Find App ID and App Secret

Facebook Find App ID and App Secret

Also in the config.php file, you will need to update your current Facebook app URL to ensure everything works properly.

PHP Load Facebook User Profile Information Via Graph API and FQL

<?php
	require 'src/facebook.php';
	require 'config.php';
 
	$facebook = new Facebook(array(
	  'appId'  => $appId,
	  'secret' => $secret,
	));
 
	$user_id = $facebook->getUser();
?>
<!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>Untitled Document</title>
</head>
 
<body>
<div style="font-size:13px; color:#333333; line-height:24px;">
<?php
	if($user_id) 
	{
      	try 
		{
			$user_profile = $facebook->api('/me','GET');
			echo "<h1>1. User profile returned from Graph API</h1>";
			echo "Profile ID: " . $user_profile['id'] . "<br>";
			echo "Name: " . $user_profile['name'] . "<br>";
			echo "Email: " . $user_profile['email'] . "<br>";
			echo "Locale: " . $user_profile['locale'] . "<br>";
			echo "Timezone: " . $user_profile['timezone'] . "<br>";
 
			echo "<h3>All information in the user_profile array</h3>";
			echo "&lt;pre&gt;";
			var_dump($user_profile);
			echo "&lt;/pre&gt;";
 
			echo "<h1>2. User profile returned from FQL query</h1>";
 
			$fql = 'SELECT name, username, pic_square, locale, email, friend_count from user where uid = ' . $user_id;
 
			$ret_obj = $facebook->api(array(
									   'method' => 'fql.query',
									   'query' => $fql,
									 ));
 
			echo 'Name: ' . $ret_obj[0]['name'] . '<br>';
			echo 'Username: ' . $ret_obj[0]['username'] . '<br>';
			echo 'Profile picture: ' . $ret_obj[0]['pic_square'] . '<br>';
			echo 'Email: ' . $ret_obj[0]['email'] . '<br>';
			echo 'Locale: ' . $ret_obj[0]['locale'] . '<br>';
			echo 'Total friends: ' . $ret_obj[0]['friend_count'] . '<br>';
      	} 
		catch(FacebookApiException $e) 
		{
		  $login_url = $facebook->getLoginUrl(array(
			'redirect_uri'         	=> $facebook_app_url,
			'scope'      			=> "email,publish_stream,user_hometown,user_location,user_photos,friends_photos,
									user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos"));
 
			echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";
      	}   
    } 
	else 
	{
      $login_url = $facebook->getLoginUrl(array(
		'redirect_uri'         	=> $facebook_app_url,
		'scope'      			=> "email,publish_stream,user_hometown,user_location,user_photos,friends_photos,
								user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos"));
 
		echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";
    }
?>
</div>
</body>
</html>

Check out Facebook FQL user to see which information you may access.

+ View demonstration on my Facebook page
+ Download the PHP source code above as well as the current latest Facebook PHP SDK

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.google.com/ Justis

    Your artciels are for when it absolutely, positively, needs to be understood overnight.

  • http://www.bestelectricrazorscooterreviews.com/category/electric-razor-scooter-reviews/ scooters

    Hello, Neat post. There is an issue with your website in internet explorer, might check this? IE still is the market chief and a good portion of people will leave out your excellent writing due to this problem.

  • hoanhuynh

    Hello, I received few comments regrading to IE issue.
    Could you kindly let me know which version of IE you’re using in order for me double check the compatibility issues.
    Thank you very much!
    H2.

  • http://www.thepersonalisedtouch.com/engraved-gifts/engraved-drinks-items/engraved-hip-flasks Engraved Hip Flask

    Wonderful issues altogether, you simply received a brand new reader. What may you recommend about your put up that you simply made some days in the past? Any sure?