How to Retrieve Facebook Profile Information using PHP SDK v3.2

Facebook has become the largest used social network these days and many businesses directly connect with their customers by using Facebook Apps and Business Pages. But there are still many people looking for methods for using Facebook Apps to give their users a convenient way of registering on their website. For this purpose, a set of tools is being provided by Facebook, called SDK (Software Development Kit). These tools let developers interact with Facebook’s servers and retrieve useful information and perform any allowed actions on behalf of user to give enhanced experience to their users. So this tutorial is about how you can Retrieve Basic Profile Information of a user by using Facebook PHP SDK.

Retrieve Profile Information Facebook PHP SDK v3

Facebook PHP SDK lets us do the following:

  1. Retrieving Profile Information, Custom Friends List, Messages, News Feed Items, Liked Pages, Profile Activity, Apps Lists and more.
  2. Posting Status Updates (text,pictures,videos,links), Sending App Requests and Suggesting Apps to Friends.
  3. And much more..

We can do a lot more with this SDK, but in this post we are intended to login to Facebook & Retrieve Basic Profile. Here are the steps for doing this, but first let’s see how it works:

How it Works:


Using Facebook API is pretty much simple. We need to setup a few parameters for initializing Facebook PHP class.

App Login Credentials:

$app_id = “your_app_id”;
$app_secret = “your_app_secret”;

App ID and App Secret Key:

Above two parameters are passed into Facebook PHP Class for initializing it. To get your App Id and App Secret, please refer to creating Facebook app and finding app id and app secret.

Next two required parameters are:
$app_permissions = “public_profile,email”; (if no value is entered in this parameter, the default will be public_profile)
$page_url = ‘http://demos.earlysandwich.com/retrieve-profile-information-facebook-php-sdk-v3/example.php’;

App Permissions:

App permissions are required by Facebook App for fetching particular information from user’s profile or performing actions on behalf of the user, for example fetching basic info, posting a status update, sending app requests to their friends, etc. Once the user authorizes your app with the permissions you asked, then your app will be added in the user’s app list and user will not have to authorize your app again and again. If sometime later you add one or more (not already authorized) permissions in your permissions list, then the user will have to authorize those permissions (by the same process) again before proceeding.

Here are some of the basic permissions for performing various actions via SDK:

Permission name Description
public_profile
  • ID
  • First Name
  • Gender
  • Last Name
  • Link
  • Locale
  • Full Name
  • Timezone
  • Updated Time
  • Verified
email Gives access to your app for getting the user’s email address associated with their Facebook account.
publish_actions Provides access to your app for publishing a story to the user’s news feed or updating user’s status. This includes text updates, photos, videos, and links.
user_friends Provides you a list of user’s friends who have already used your app.

For a complete list of available permissions, please refer to Facebook Documentation.

Redirect URL:

After authorization of your app from the user, they will be redirected back to your website on the specified redirect URL (in config.php). For example: If your redirect URL is: http://example.com/example.php then Facebook will redirect user to http://example.com/example.php?code=token_from_facebook. Be sure you put here the URL of the same file in which your code is working. After a user comes back to your site, this script will get token from URL and will use it to send authorized request to Facebook for Fetching Profile Information of User.

Usage:


Config.php file:

We will start with a very basic code here. First of all we will create a file config.php and put the following code in it:

Above configurations only include credentials for communicating with the Facebook API.

fetch-profile-info.php file:

Now let’s set up another file named fetch-profile-info.php. Here is its code:

The above code uses PHP Include and Require Functions to include both necessary files. And then initializes class: Facebook. Next it gets code parameter from the URL and sends final request to Facebook (via API) for fetching required information.

Checking Response from Facebook:

When a user uses your app and authorizes it on Facebook, then the user is redirected back to your website with a token in the URL. This token is created by Facebook for temporarily allowing app owners to perform different allowed actions on the user’s profile for a limited time (normally token expires in 60 minutes). To get this token, we use $_GET[‘code’] in the code below:

fetch-profile.info.php (continued):

Above example will check if code parameter is present in the URL. If so, then it will test if the token is still valid or expired. For this we use $facebook->api(‘/me’) method. This method will query Facebook asking for basic info of the user. /me means we are calling the API for fetching profile information of the user currently using the app. You may replace /me with /id_of_any_user for getting that particular user’s information. If the token is not valid or expired, then we redirect user to Facebook for getting a fresh token. All data retrieved from Facebook is stored now in $user_profile array. And can easily be printed using print_r($user_profile);.

If you want to show a logout button to the user, just uncomment the code given for log out URL. Clicking on the log out button will log out the user, kill the session created by the app, and make a temporary token code to expire on Facebook’s servers.

See this script in action. And download whole of the above script below.

Retrieve-profile-information-facebook-php-sdk-v3
Retrieve-profile-information-facebook-php-sdk-v3
retrieve-profile-information-facebook-php-sdk-v3.zip
163.6 KiB
67 Downloads
Details

3 Replies to “How to Retrieve Facebook Profile Information using PHP SDK v3.2”

  1. lewis says:

    hi! im trying to integrate this into my site! works great but have come up against an issue with sessions. I can get the script to log the user in with checking against DB and move to the logged in page, however the logged in session ($_SESSION[‘loggedin’] = 1) appears to be lost when I try to navigate to another page. I also lose the rest of the session variables that I set during the login.
    My normal login method works fine and session variables are maintained throughout.

    Are you able to give me any suggestions as to why this might be happening?

    1. Usman Haider says:

      Hello, thanks for raising the issue. Here is its solution:
      Try adding the following line after including the sdk file:
      session_start();

      Because sessions need to be started before they can be set and called in the code.

      I hope this solves your problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top