· 8 years ago · Jan 26, 2018, 06:34 PM
1<?php
2//fb_connect.php
3//Author: Graham McCarthy, HitSend Inc., September 29th, 2010
4//Email: graham@hitsend.ca
5//Description: facebook connect library, connects to facebook and
6// stores all the required information in return variables
7//grab the facebook api php file
8include(APPPATH.'libraries/facebook/facebook.php');
9
10class Fb_connect {
11//declare variables
12private $_obj;
13private $_api_key = NULL;
14private $_secret_key = NULL;
15
16public $user = NULL;
17public $user_id = FALSE;
18
19public $fbLoginURL = "";
20public $fbLogoutURL = "";
21
22public $fb = FALSE;
23public $fbSession = FALSE;
24public $appkey = 0;
25
26//constructor method.
27function Fb_connect()
28{
29//Using the CodeIgniter object, rather than creating a copy of it
30$this->_obj =& get_instance();
31
32//loading the config paramters for facebook (where we stored our Facebook API and SECRET keys
33$this->_obj->load->config('facebook');
34//make sure the session library is initiated. may have already done this in another method.
35$this->_obj->load->library('session');
36
37//
38$this->_api_key = $this->_obj->config->item('facebook_api_key');
39$this->_secret_key = $this->_obj->config->item('facebook_secret_key');
40
41$this->appkey = $this->_api_key;
42
43//connect to facebook
44$this->fb = new Facebook(array(
45'appId' => $this->_api_key,
46'secret' => $this->_secret_key,
47'cookie' => true,
48));
49
50//store the return session from facebook
51$this->fbSession = $this->fb->getSession();
52
53$me = null;
54// If a valid fbSession is returned, try to get the user information contained within.
55if ($this->fbSession) {
56try {
57//get information from the fb object
58$uid = $this->fb->getUser();
59$me = $this->fb->api('/me');
60
61$this->user = $me;
62$this->user_id = $uid;
63
64} catch (FacebookApiException $e) {
65error_log($e);
66}
67}
68
69// login or logout url will be needed depending on current user state.
70//(if using the javascript api as well, you may not need these.)
71if ($me) {
72$this->fbLogoutURL = $this->fb->getLogoutUrl();
73} else {
74$this->fbLoginURL = $this->fb->getLoginUrl();
75}
76} //end Fb_connect() function
77} // end class