Example 3 – retrieving data from Facebook

In this example, we will use the CI built-in model to retrieve data from Facebook.

The example displays a Facebook user name and picture and displays the user's Facebook friends.

This example uses Facebook PHP SDK as a CI library. It can be downloaded from https://github.com/facebook/php-sdk. For more information, refer to Chapter 4, Libraries.

This example will be constructed from the following controllers, model, and view:

  • application/controllers/fbpage.php: This controller loads the model fbmodel
  • $this->load->model('fbmodel'): This controller renders the view fbview, which displays the user's Facebook picture and name, and table, which contains the user's friends' names and links to their profiles
  • application/models/fbmodel.php: This model contains functions that retrieve data from Facebook
  • application/views/fbview.php: This view displays Facebook data

Let us assume the URI to the project root is http://mydomain.com/myproject and http://mydomain.com/myproject/fbpage.

Note

The source code is provided with this book via URLs.

The controller file

The controller PHP file is located at application/controllers/fbpage.php.

The controller is responsible for getting the access token from Facebook and redirecting the Facebook user to the Facebook login page to confirm the Facebook app's permission.

The controller is also responsible for getting the Facebook user's details and friends via the model and rendering the view page accordingly.

For more information about Facebook API usage and development, please refer to the Facebook developer page available at http://developers.facebook.com/.

The following are the code and inline explanations:

<?php
class Fbpage extends CI_Controller {
  public function __construct() {
    parent::__construct();
    // Extremely important!!!
    // Due to the fact that the CI handles classes for // $_GET, $_POST, and $_COOKIE parse_str is called to // copy the variables sent by Facebook to the $_REQUEST var, // so that the Facebook SDK can do its checks.
    // This is done in order to avoid infinite redirect loop.
    parse_str($_SERVER['QUERY_STRING'], $_REQUEST);
    }
  // This method retrieves Facebook data of a Facebook user and // displays personal details and some of his  friends.
  // It checks if a Facebook token is valid, if it's valid, // then it displays his details, otherwise it produces // the token.
  public function index() {
    $a_config = array('appId' => $fb_API, 'secret'=> $fb_secret, 'cookie' => true);
    $this->load->library('facebook', $a_config);
    // Checking if the user is logged in and confirms // the app's permissions.
    if ($user = $this->facebook->getUser()) {
      // Get the Facebook token
      $access_token = $this->facebook->getAccessToken();
      // Loading the fbmodel
      $this->load->model('fbmodel'),
      // Updating the token
      $this->fbmodel->set_token($access_token);
      // Get a Facebook user's profile details
      $user_profile = $this->fbmodel->get_user_profile();
      // Getting the Facebook user ID
      $uid = $user_profile['id'];
      
      // Retrieving a Facebook user's details
      $me = $this->fbmodel->get_me_by_fql($uid);
      // Get a Facebook user's friends
      $friends = $this->fbmodel->get_friends();
      $view_params = array('me' => $me, 'friends' => $friends);
      // Loading the view
      $this->load->view("fbview", $view_params);
      } else {
      // The Facebook parameters for the Facebook login URL, // where scope consists the Facebook app's permissions.
      $a_params = array ('fbconnect' => 0, 'scope' => offline_access, publish_stream', 'cookie' => true);
      // The Facebook login URL page
      $login_url= $this->facebook->getLoginUrl($a_params);
      // Redirecting the Facebook user to the login URL.
      // After the Facebook user confirms the permissions // required by the app; he is redirected back to the // index page.
      header('Location:'. $login_url);
      }
    }
  }

The model file

The model PHP file is located at application/models/fbmodel.php.

The model is responsible for interacting with the Facebook SDK and retrieving the Facebook user's details and friend lists. The model uses the Facebook FQL mechanism.

For more information about Facebook API usage and development, please refer to the Facebook developer page available at http://developers.facebook.com/.

The code and inline explanations are as follows:

<?php
class fbmodel extends CI_Model {
  // The Facebook app's token
  private $token;
  public function __construct() {
    // Call the model constructor
    parent::__construct();
    }
  
  // This method sets the model class's private token value
  public function set_token($token) {
    $this->token = $token;
    }
  
  // This method returns an array, which contains the Facebook user // profile.
  public function get_user_profile() {
    // Getting the CI main class to get access to the Facebook // library.
    $ci =& get_instance();
    
    // Getting the Facebook user's profile
    $user_profile = $ci->facebook->api('/me'),
    return $user_profile;
    }
  
  // This method returns an array, which contains a Facebook user's // details.
  public function get_me_by_fql($uid) {
    // Getting the CI main class to get access to the Facebook // library.
    $ci =& get_instance();
    // The SQL query to send to Facebook $fql = SELECT uid, name, // pic_big FROM user WHERE uid=" $uid;
    $param = array('method' => 'fql.query', 'query' => $fql, 'callback' => ''),
    
    // Getting the Facebook user's details
    $fqlResult = $ci->facebook->api($param);
    // Returning an array, which contains the required details
    return $fqlResult;
    }
  
  // This method returns an array of a Facebook user's friend.
  public function get_friends() {
    // Getting the CI main class to get access to the Facebook // library
    $ci =& get_instance();
    // Getting the Facebook user's friends
    $friends = $ci->facebook->api('/me/friends'),
    
    // Returning an array, which contains a Facebook user's friend
    return $friends;
    }
  }

The view file

The view PHP file is located at application/views/fbview.php.

This view file displays a Facebook user's details and a table with their friend details.

The code and inline explanations are as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My facebook details</title>
</head>
<body>

<div id="my_details">
  <div id="picture"><img src="<?=$me[0]['pic_big'] ?>"></div>
  <div id="my_name"><?=$me[0]['name'] ?></div>
</div>
<table>
<tr>
  <th>Name</th>
  <th>Link to friend</th>
</tr>
<?php foreach ($friends['data'] as $friend): ?>
<tr>
  <td><?=$friend['name']?></td>
  <td><a href='http://www.facebook.com/<?=$friend["id"]?>'>To friend</a></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.186.143