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.

<?php
// Basic Search with name and gives full name, age, gender, profile, locale

	error_reporting(E_ALL);
	ini_set('display_errors','On');
	
	if($_SERVER['REQUEST_METHOD'] == 'POST')
	{
		
		if(!session_id()) {
			session_start();
		}
		
		// Include the required dependencies.
		require_once( 'inc/facebook/autoload.php' );
		require_once( "inc/facebook/Facebook.php" );
		
		
		$fb = new Facebook\Facebook([
				'app_id'                => 'YOUR APP ID',
				'app_secret'            => 'YOUR APP SECRET',
				'default_graph_version' => 'v2.3',
				'cookie' => true
		]);
		
		$accessToken = 'YOUR ACCESS TOKEN';
		
		$session = $fb->setDefaultAccessToken($accessToken);
		
		$response = $fb->get("/search?q={$_POST['name']}&type=user", $accessToken);
		
		$responseUsers = json_decode($response->getGraphEdge());
		 
		foreach($responseUsers as $value)
		{
			$users[] = (array) $value;
		}
		
		foreach($users as $user){
			
			$theResult = $fb->get("/{$user['id']}?fields=id,name,age_range,gender,locale,link,birthday", $accessToken);
			$results[] = (array)json_decode($theResult->getGraphNode());
			
		}
		
	}	
	
?>

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.

<?php 
	include "search.php";
?>

<html>

<head>
	<title>Search People on FB</title>
	<style>
		td{
			border-top: 1px solid black;
			border-bottom: 1px solid black;
		}
	</style>
</head>

<body>

	<br><a href="/searchFriends.php">Search Friends</a><br><br>

	<form method="POST" action="">
		Enter the Name:
		<input type="text" name="name">
		
		<input type="submit" value="search">
	
	</form>

	<?php if(!empty($results)){?>
	
		<table style="border:3px solid black;">
		
			<tr>
				<td>Id</td>
				<td>Name</td>
				<td>Gender</td>
				<td>Locale</td>
				<td>Link</td>
				<td>Age Range</td>
			</tr>
			
			<?php foreach($results as $user){?>
				
				<tr>
					<td><?php if(!empty($user['id'])){echo $user['id'];}?></td>
					<td><?php if(!empty($user['name'])){echo $user['name'];}?></td>
					<td><?php if(!empty($user['gender'])){echo $user['gender'];}?></td>
					<td><?php if(!empty($user['locale'])){echo $user['locale'];}?></td>
					<td><?php if(!empty($user['link'])){echo "<a href={$user['link']}>View Profile</a>";}?></td>
					<td><?php if(!empty($user['age_range'])){$age = (array)$user['age_range']; 
								if(!empty($age['min'])){echo $age['min'];} if(!empty($age['max'])){echo " - ".$age['max'];}}?></td>
				</tr>
				
			<?php }?>
		</table>
	
	
	<?php }?>
	
	
		<?php if(!empty($results)){
		echo "<pre>";
		print_r($results);
		die();}?>
</body>

</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!

Related Post

About the Author

Rashika Raina

Rashika is working as Business Development Executive at Regur technology Solution. She has a zeal to learn & grow and passion for work in any competitive situation. Her interest lies in Sales, Marketing Strategies, International Marketing, Business development. Apart from this, she has a good hold in communication, presentation and Market research skills.