Find Facebook Users By Names

Facebook is the largest social media website on the Internet. Thus, it provides a lot of options for you to use Facebook with your website and develop some awesome functionalities using Facebook API.

You can create your own app on Facebook and let all your users use it by their Facebook account. To create an app, follow the below given guidelines.

How to create a Facebook app to use it with Facebook API?

Step – 1

Go to https://developers.facebook.com/

Step -2

Click on Sign In

Step – 3

Enter the credentials of your Facebook account and this will log you in to the Facebook Developer’s account.

Step-4

Click on ‘Add a new app’

Step – 5

It will prompt you for your app’s name. Enter the desired name.

Step – 6

Now, follow the steps and select the platform for which you are developing this app. Select one out of the four options viz. iOS, Android, Facebook Canvas, Website

Step – 7

This is it! You will now be redirected to the app’s dashboard and you can get the app ID there.

So now, it is very clear about how to create your Facebook application. Now let’s explore on getting the data of the users from Facebook

Getting user datafrom the Facebook API:

Now, we will develop a code and set up the Facebook application in such a way that we can search users by their name. For those users, which are accessing our application and have given permission to access their profile, we will get all the permitted details.

For the rest of the users, Facebook API provides the details like ID, Facebook Profile and full name. Thus, with this code you can search people with their names.

Note: Here, in this demo code, we are using API Test application for website. The working environment is Debian over Vagrant.

Step – 1

Go to the dashboard of the Facebook application and then click on ‘Settings’ .

Step – 2

Enter your App domain i.e. the site which is going to access the application and enter the site URL.

So, for example if your domain is fbapi.com then your URL will be http://fbapi.com.

Step – 3

Now, download the Facebook SDK for PHP and put it in your project folder to get access to the Facebook API.

Step – 4

Now, create a file and save it as search.php. Put the below given code snippet to your search.php file. For this, you will need your access token to access the Facebook API.

  1. <?php
  2. // Basic Search with name and gives full name, age, gender, profile, locale
  3.  
  4. 	error_reporting(E_ALL);
  5. 	ini_set('display_errors','On');
  6.  
  7. 	if($_SERVER['REQUEST_METHOD'] == 'POST')
  8. 	{
  9.  
  10. 		if(!session_id()) {
  11. 			session_start();
  12. 		}
  13.  
  14. 		// Include the required dependencies.
  15. 		require_once( 'inc/facebook/autoload.php' );
  16. 		require_once( "inc/facebook/Facebook.php" );
  17.  
  18.  
  19. 		$fb = new Facebook\Facebook([
  20. 				'app_id'                => 'YOUR APP ID',
  21. 				'app_secret'            => 'YOUR APP SECRET',
  22. 				'default_graph_version' => 'v2.3',
  23. 				'cookie' => true
  24. 		]);
  25.  
  26. 		$accessToken = 'YOUR ACCESS TOKEN';
  27.  
  28. 		$session = $fb->setDefaultAccessToken($accessToken);
  29.  
  30. 		$response = $fb->get("/search?q={$_POST['name']}&type=user", $accessToken);
  31.  
  32. 		$responseUsers = json_decode($response->getGraphEdge());
  33.  
  34. 		foreach($responseUsers as $value)
  35. 		{
  36. 			$users[] = (array) $value;
  37. 		}
  38.  
  39. 		foreach($users as $user){
  40.  
  41. 			$theResult = $fb->get("/{$user['id']}?fields=id,name,age_range,gender,locale,link,birthday", $accessToken);
  42. 			$results[] = (array)json_decode($theResult->getGraphNode());
  43.  
  44. 		}
  45.  
  46. 	}	
  47.  
  48. ?>

Step – 5

Now create the index.php file which will have the front end for searching and will display the results in the tabular format.

  1. <?php 
  2. 	include "search.php";
  3. ?>
  4.  
  5. <html>
  6.  
  7. <head>
  8. 	<title>Search People on FB</title>
  9. 	<style>
  10. 		td{
  11. 			border-top: 1px solid black;
  12. 			border-bottom: 1px solid black;
  13. 		}
  14. 	</style>
  15. </head>
  16.  
  17. <body>
  18.  
  19. 	<br><a href="/searchFriends.php">Search Friends</a><br><br>
  20.  
  21. 	<form method="POST" action="">
  22. 		Enter the Name:
  23. 		<input type="text" name="name">
  24.  
  25. 		<input type="submit" value="search">
  26.  
  27. 	</form>
  28.  
  29. 	<?php if(!empty($results)){?>
  30.  
  31. 		<table style="border:3px solid black;">
  32.  
  33. 			<tr>
  34. 				<td>Id</td>
  35. 				<td>Name</td>
  36. 				<td>Gender</td>
  37. 				<td>Locale</td>
  38. 				<td>Link</td>
  39. 				<td>Age Range</td>
  40. 			</tr>
  41.  
  42. 			<?php foreach($results as $user){?>
  43.  
  44. 				<tr>
  45. 					<td><?php if(!empty($user['id'])){echo $user['id'];}?></td>
  46. 					<td><?php if(!empty($user['name'])){echo $user['name'];}?></td>
  47. 					<td><?php if(!empty($user['gender'])){echo $user['gender'];}?></td>
  48. 					<td><?php if(!empty($user['locale'])){echo $user['locale'];}?></td>
  49. 					<td><?php if(!empty($user['link'])){echo "<a href={$user['link']}>View Profile</a>";}?></td>
  50. 					<td><?php if(!empty($user['age_range'])){$age = (array)$user['age_range']; 
  51. 								if(!empty($age['min'])){echo $age['min'];} if(!empty($age['max'])){echo " - ".$age['max'];}}?></td>
  52. 				</tr>
  53.  
  54. 			<?php }?>
  55. 		</table>
  56.  
  57.  
  58. 	<?php }?>
  59.  
  60.  
  61. 		<?php if(!empty($results)){
  62. 		echo "<pre>";
  63. 		print_r($results);
  64. 		die();}?>
  65. </body>
  66.  
  67. </html>

After configuring the application and your website correctly, you will be able to search people with their names.

There are multiple usage scenarios where having a Facebook API integration with your site can greatly increase your functionality. We’ve had considerable success integrating APIs like this with business websites – with a good understanding of web development, or by hiring an expert developer, you’ll be able to do this as well!