Home arrow PHP arrow Page 2 - Facebook PHP API Applications: A Second Look

Understanding the Facebook Graph API - PHP

This is the second part of the Facebook PHP API tutorial. The first part ran on August 11. It covered the most basic features, such as adding the basic Facebook “Like” button, the “Recommendations” plugin and registering your applications in Facebook to get an Application ID and Secret key. In this part, we'll delve deeper.

TABLE OF CONTENTS:
  1. Facebook PHP API Applications: A Second Look
  2. Understanding the Facebook Graph API
  3. Is the user logged in?
  4. Final Facebook PHP API Script - Display basic user information
By: Codex-M
Rating: starstarstarstarstar / 5
August 16, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As a Facebook developer, it is important that you understand the Facebook Graph. Facebook also call this the "social graph."

Say, for example, you want to retrieve the following data from a Facebook user's profile:

  • User ID
  • Name
  • Gender
  • Facebook URL link

Facebook uses JSON objects when presenting user profile data. A user's profile can be obtained via their Facebook graph's sub-domain

According to Facebook, every object in the social graph contains a unique ID. So all Facebook users have a unique ID assigned to them.

Another thing you need to know is that you can access a Facebook user's information via the graph URL; you just need to append the Facebook user's ID. For example:

https://graph.facebook.com/ID

where "ID" is the actual user ID. The data are presented as JSON objects. For example, if your user ID is 747166266, you can access it in the address bar as follows:

https://graph.facebook.com/747166266

See the screen shot below:

The required get_facebook_cookie_function

This is the start of the PHP script discussion. We're going to analyze the sample script provided by the Facebook API. The first step is to get the Facebook cookie. This is will be used by the succeeding scripts to check whether or not the user has logged in to your website using their Facebook account.

To do this in your PHP web application, paste the following code at the top of your PHP scripts (the very first line of code):

<?php
define('FACEBOOK_APP_ID', '***YOUR FACEBOOK APPLICATION ID HERE***');
define('FACEBOOK_SECRET', '***YOUR FACEBOOK SECRET APPS KEY HERE***');
function get_facebook_cookie($app_id, $application_secret) {
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '"'), $args);
  ksort($args);
  $payload = '';
  foreach ($args as $key => $value) {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
    }
  }
  if (md5($payload . $application_secret) != $args['sig']) {
    return null;
  }
  return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
?>

You need a Facebook Application ID and secret apps key, to be placed under "Define."



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: