Advertisement
Guest User

Untitled

a guest
Dec 16th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Facebook Login - Log in via Facebook
  4.  * @author Daniel15 <dan.cx>
  5.  */
  6.  
  7. class FacebookLogin
  8. {
  9.     const AUTHORIZE_URL = 'https://graph.facebook.com/oauth/authorize';
  10.     const TOKEN_URL = 'https://graph.facebook.com/oauth/access_token';
  11.     const PROFILE_URL = 'https://graph.facebook.com/me';
  12.    
  13.     private $client_id;
  14.     private $client_secret;
  15.     private $my_url;
  16.     private $user_data;
  17.    
  18.     /**
  19.      * Create an instance of the FacebookLogin class
  20.      */
  21.     public function __construct($client_id, $client_secret)
  22.     {
  23.         $this->client_id = $client_id;
  24.         $this->client_secret = $client_secret;
  25.         $this->my_url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
  26.     }
  27.    
  28.     /**
  29.      * Do a Facebook login - either redirects to Facebook or reads the returned result
  30.      */
  31.     public function doLogin()
  32.     {
  33.         // Are we not returning from Facebook (ie. starting the login)?
  34.         return !isset($_GET['code']) ? $this->startLogin() : $this->verifyLogin();
  35.     }
  36.    
  37.     /**
  38.      * Start a login with Facebook - Redirect to their authentication URL
  39.      */
  40.     private function startLogin()
  41.     {
  42.         $data = array(
  43.             'client_id' => $this->client_id,
  44.             'redirect_uri' => $this->my_url,
  45.             'type' => 'web_server',
  46.         );
  47.        
  48.         header('Location: ' . self::AUTHORIZE_URL . '?' . http_build_query($data));
  49.         die();
  50.     }
  51.    
  52.     /**
  53.      * Verify the token we receive from Facebook is valid, and get the user's details
  54.      */
  55.     private function verifyLogin()
  56.     {
  57.         $data = array(
  58.             'client_id' => $this->client_id,
  59.             'redirect_uri' => $this->my_url,
  60.             'client_secret' => $this->client_secret,
  61.             'code' => $_GET['code'],
  62.         );
  63.  
  64.         // Get an access token
  65.         $result = @file_get_contents(self::TOKEN_URL . '?' . http_build_query($data));
  66.         parse_str($result, $result_array);
  67.        
  68.         // Make sure we actually have a token
  69.         if (empty($result_array['access_token']))
  70.             throw new Exception('Invalid response received from Facebook. Response = "' . $result . '"');
  71.        
  72.         // Grab the user's data
  73.         $this->access_token = $result_array['access_token'];
  74.         $this->user_data = json_decode(file_get_contents(self::PROFILE_URL . '?access_token=' . $this->access_token));
  75.         return $this->user_data;
  76.     }
  77.    
  78.     /**
  79.      * Helper function to get the user's Facebook info
  80.      */
  81.     public function getUser()
  82.     {
  83.         return $this->user_data;
  84.     }
  85. }
  86.  
  87. //code paste
  88. $facebook = new FacebookLogin('*******************', '*****************************');
  89. $user = $facebook->doLogin();
  90. echo 'User\'s URL: ', $user->link, '<br />';
  91. echo 'User\'s name: ', $user->name, '<br />';
  92. echo 'Full details:<br /><pre>', print_r($user, true), '</ pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement