Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Facebook Graph API Facebook Load User Profile Via Graph API And FQL Query

Facebook Load User Profile Via Graph API And FQL Query

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>

<?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

Sep 28, 2011Hoan Huynh

Source Code Demo page

PHP CURL Post To HTTPS WebsiteFacebook Publish To Wall With External Link And Track Callback
You Might Also Like:
  • Load And Save Facebook Profile Picture Of User
  • PHP Change Facebook Profile Picture With Graph API
  • PHP Check If User Like Facebook Page
  • Get or Find Facebook Profile Id Number
  • Add More Extra Informations Or Fields To WordPress User Profile
  • PHP Load And Save Facebook Friend List
  • PHP Load Facebook Albums And Save To MySQL Database
  • Facebook Removed View App Profile Page link For New Apps
  • Create Custom Update Profile Page For WordPress Users
  • How To Ask People To Like Your Facebook Page On The Landing Page
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago Facebook Graph APIFacebook, Facebook FQL, getLoginUrl393
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,469 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,844 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,661 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,004 views
C# Read Json From URL And Parse/Deserialize Json
11,737 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development