Ce document a été mis à jour.
La traduction en Français (France) n’est pas encore terminée.
Anglais mis à jour : 20 déc. 2019
Français (France) mis à jour : 9 mai 2017

Getting Started

This tutorial shows you how to implement the Facebook PHP SDK and get your name from a Node of the Facebook Graph.

Before You Start

You will need:

Install the SDK

To install the Facebook PHP SDK run the following command (this example uses composer) in a terminal window on your server, in your project folder:

my-server:my-php-project$ composer require facebook/graph-sdk

This command will install the SDK in the /vendor folder of your project.

my-server:my-php-project$ ls -al vendor/facebook/
total 0
drwxr-xr-x  3 khoover  1876110778   96 Dec 12 10:55 .
drwxr-xr-x  5 khoover  1876110778  160 Dec 16 13:19 ..
drwxr-xr-x  7 khoover  1876110778  224 Dec 12 10:55 graph-sdk
Learn more about the Facebook PHP SDK.

Get My Name

In this example you will be getting data from a UserNode, /me (this represents the person who requested the access token), with a User access token and the default permission, public_profile.

By default, you have access to the public_profile permissions so you do not need to request any other permission when requesting your access token.

To find data using a Facebook User ID replace "/me" with your User ID, for example, /499633188.

A Sample index.php File

<!DOCTYPE html>
<html>
<head>
  <title>
    My Name 
  </title>
</head>

<body>
  <h1>Get My Name from Facebook</h1>

<?php

require_once __DIR__ . '/vendor/autoload.php';   

$fb = new \Facebook\Facebook([
  'app_id' => '{your-app-id}',           //Replace {your-app-id} with your app ID
  'app_secret' => '{your-app-secret}',   //Replace {your-app-secret} with your app secret
  'graph_api_version' => 'v5.0',
]);


try {
   
// Get your UserNode object, replace {access-token} with your token
  $response = $fb->get('/me', '{access-token}');

} catch(\Facebook\Exceptions\FacebookResponseException $e) {
        // Returns Graph API errors when they occur
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
        // Returns SDK errors when validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$me = $response->getGraphUser();

       //All that is returned in the response
echo 'All the data returned from the Facebook server: ' . $me;

       //Print out my name
echo 'My name is ' . $me->getName();

?>

</body>
</html>

Next Steps

Visit our Code Samples Guide to learn how to get an Access Token, your hometown, or your Facebook posts.