Created
September 21, 2012 11:13
-
-
Save ilmsg/3760930 to your computer and use it in GitHub Desktop.
Codeigniter, Facebook SDK, PHP: Using Facebook SDK to get user FB id, like gate and/or check login status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
BE SURE TO INCLUDE THE FACEBOOK SDK FILES, DROP THE FOLLOWING FILES INTO THE 'third_party' FOLDER IN CODEIGNITER: | |
base_facebook.php | |
facebook.php | |
fb_ca_chain_bundle.crt | |
*/ | |
// ============= MODEL FILE (facebook_api_model.php) | |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Facebook_api_model extends CI_Model | |
{ | |
// -------------------------------- GET USERS FACEBOOK ID, CHECK LOGGED IN STATUS AND LIKE GATE | |
function call_facebook() | |
{ | |
/*===================== FACEBOOK API STUFF STARTS ====================*/ | |
// INCLUDE FACEBOOK SDK | |
require 'application/third_party/facebook.php'; | |
// THE BELOW LINES ARE USED TO CREATE THE $auth_url VAR - THIS VARIABLE IS RESPONSIBLE FOR REDIRECTION IF YOU ARE NOT SIGNED IN | |
$app_id = '414815291884674'; | |
// $canvas_page = "http://www.facebook.com/pages/Proximity/160329604032006?sk=app_414815291884674"; | |
$canvas_page = "http://www.facebook.com/kenco/?sk=app_YOUR APP ID"; | |
//$canvas_page = "http://fb.kraft-hosting.net/kenco/uken/adoration"; | |
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page); | |
// INIT FACEBOOK OBJECT WITH APP KEYS | |
$facebook = new Facebook(array( | |
'appId' => 'YOUR APP ID HERE', | |
'secret' => 'YOUR APP SECRET' | |
)); | |
// BELOW LINE GETS USER ID AND STORES TO VAR | |
$user = $facebook->getUser(); | |
// STORE USER FACEBOOK ID IN SESSION REGARDLESS OF WHETHER OR NOT PAGE IS LIKED | |
if(isset($user)){ | |
$this->session->set_userdata('fb_id', $user); | |
}else{ | |
$this->session->set_userdata('fb_id', ''); | |
} | |
// BELOW LINE GETS THE SIGNED REQUEST TO FIND OUT IF A USER HAS LIKED THE PAGE OR NOT | |
$signed_request = $facebook->getSignedRequest(); | |
// REDIRECT IF USER VAR IS EMPTY/NOT SIGNED IN | |
if($user == '' || $user == NULL || $user == 'NULL') | |
{ | |
if($user == '0' || $user == 0) | |
{ | |
echo '<script type="text/javascript">window.parent.location = "'.$auth_url.'"</script>'; | |
exit; | |
} | |
// BECAUSE FACEBOOK APPS ARE IN IFRAMES YOU NEED TO DO A PARENT WINDOW REDIRECT USING JAVASCRIPT | |
echo '<script type="text/javascript">window.parent.location = "'.$auth_url.'"</script>'; | |
exit; | |
//header("Location: ".$auth_url); | |
} | |
// GET LIKE STATUS | |
$likeStatus = empty($signed_request["page"]["liked"]) ? false : (bool)$signed_request["page"]["liked"]; | |
return $likeStatus; | |
/*===================== FACEBOOK API STUFF ENDS ====================*/ | |
} | |
// -------------------------------- LIKE GATE ONLY | |
function like_gate() | |
{ | |
/*===================== FACEBOOK API STUFF STARTS ====================*/ | |
// INCLUDE FACEBOOK SDK | |
require 'application/third_party/facebook.php'; | |
// INIT FACEBOOK OBJECT WITH APP KEYS | |
$facebook = new Facebook(array( | |
'appId' => '258007694310745', | |
'secret' => '67736210f55f2c355fee0025e1715ef0' | |
)); | |
// BELOW LINE GETS THE SIGNED REQUEST TO FIND OUT IF A USER HAS LIKED THE PAGE OR NOT | |
$signed_request = $facebook->getSignedRequest(); | |
// GET LIKE STATUS | |
$likeStatus = empty($signed_request["page"]["liked"]) ? false : (bool)$signed_request["page"]["liked"]; | |
return $likeStatus; | |
/*===================== FACEBOOK API STUFF ENDS ====================*/ | |
} | |
} | |
/* End of file facebook_api_model.php */ | |
/* Location: ./application/models/facebook_api_model.php */ | |
// ============= CONTROLLER FILE (like_gate.php) | |
// THIS VERSION WILL ALSO CHECK IF YOU ARE LOGGED IN, WILL GET THE USERS FACEBOOK ID AND WILL REDIRECT IF YOU ARE NOT BEFIRE LIKE GATING | |
$this->likeStatus = $this->facebook_api_model->call_facebook(); | |
if($this->likeStatus){ | |
$data['main_content'] = 'liked_view'; | |
}else{ | |
$data['main_content'] = 'not_liked_view'; | |
} | |
// THIS IS PURELY FOR LIKE GATING | |
$this->likeStatus = $this->facebook_api_model->like_gate(); | |
if($this->likeStatus){ | |
$data['main_content'] = 'liked_view'; | |
}else{ | |
$data['main_content'] = 'not_liked_view'; | |
} | |
/* End of file like_gate.php */ | |
/* Location: ./application/controllers/like_gate.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment