Open In App

How to get user profile using Facebook SDK and PHP?

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Facebook Graph APIs are used heavily among Facebook developers in the web or social networking world. These are totally HTTP-based protocols that help in getting or posting data, uploading photos, and videos, and sending group requests around social graphs. The Facebook SDK helps in providing wonderful features for enhancing user interfaces with FaceBook data.

Common applications: 

  • Developing web pages with a Facebook login integration facility.
  • Simple registration process enabling more users to the website.

Pre-requisites:

  • PHP5 and above
  • Enable mbstring extension in “php.ini” file by uncommenting the line 
extension=mbstring

Facebook SDK downloads link: 

Please download the following link and save it into your working folder to include the required files or libraries in your PHP code. The PHP Facebook SDK is very easy to implement and allows access to Facebook graph APIs for developers.

https://github.com/facebookarchive/php-graph-sdk

Steps to Create Facebook App: 

  • Please go to the developers.facebook.com to create an APP ID and APP SECRET KEY and make 
    a note of it to use in the PHP code.
  • Create a new app by clicking Add New App. Enter all the required details like name, and email id, and click on Create APP ID to get APP ID and APP SECRET to access the Facebook API. You have to change some basic settings to get the desired keys. Refer to the image shown below.
  • Get the user access token from the https://developers.facebook.com/tools/explorer/APP_ID for that particular app. Refer to the image below. Use your own APP_ID in the link.  

The following image shows the snapshot for the APP ID and APP SECRET KEY.

 The following image shows the snapshot for the User Access token.

PHP code: The following example code demonstrates, how to access the current user name and print it using FaceBook SDK and Graph API using PHP.

php




<?php
 require_once 'facebook-graph-sdk/src/Facebook/Facebook.php'
 require_once 'facebook-graph-sdk/src/Facebook/autoload.php';
 require_once 'facebook-graph-sdk/src/Facebook/
               Exceptions/FacebookResponseException.php';
 require_once 'facebook-graph-sdk/src/Facebook/
               Exceptions/FacebookSDKException.php';
 require_once 'facebook-graph-sdk/src/Facebook/Helpers/
               FacebookRedirectLoginHelper.php';
  
// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
  
$appId = 'YOUR APP ID'
$appSecret = 'YOUR APP SECRET KEY'
  
$fb = new Facebook([
    'app_id' => $appId,
    'app_secret' => $appSecret,
    'default_graph_version' => 'v3.1',    
]);
  
  
// YOUR user's access token, refer 
$accessToken='YOUR ACCESS TOKEN';
  
$response= "";
  
try 
{
    $response = $fb->get('/me', $accessToken);
    $response = $fb->get('/me?fields=id, name', $accessToken);    
catch(FacebookResponseException $e)
{
     echo 'Graph returned an error:' . $e->getMessage();
     exit();
catch(FacebookSDKException $e)
{
    echo 'Facebook SDK returned an error:' . $e->getMessage();
     exit();
}
$me = $response->getGraphUser();
echo 'Logged in as (username) : ' . $me->getName().'<br/>';
?>


Output: The FaceBook user name is printed in the following output text “User name”.

  Logged in as (username) : "User name"


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads