Skip to content
Related Articles

Related Articles

Improve Article

How to get user profile using Facebook SDK and PHP?

  • Last Updated : 28 Oct, 2020

Facebook Graph APIs are used heavily among facebook developers in Tthe web or social networking world. This is totally HTTP based protocols which help in getting or posting data, uploading photos, videos, 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 required files or libraries in your PHP code. The PHP Facebook SDK is very easy to implement and allows access to to facebook graph APIs for developers.



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

Steps to create FaceBook App:

  1. Please go to link to create APP ID and APP SECRET KEY and make
    a note of it to use in the PHP code.
  2. Create new app by clicking Add New App. Enter all the required details like name, 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 the image shown below.
  3. Get the user access token from the link for that particular app. Refer the image below. Use your own APP_ID in the link.

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

The following image shows the snapshot for User Access token.

PHP code: The following example code demonstrates, how to access current user name and print it using FaceBook SDK and Graph API using 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"



My Personal Notes arrow_drop_up
Recommended Articles
Page :