Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class My_Fb
{
var $BaseUrl = 'http://someurl.com';
var $AppID = 'xxxxxxxxxxxxxxxxxxx';
var $Secret = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
var $Scope = 'email,read_insights,read_stream,publish_stream,read_mailbox,ads_management,xmpp_login,user_about_me,user_activities,user_birthday,user_education_history,user_events,user_groups,user_hometown,user_interests,user_likes,user_location,user_notes,user_online_presence,user_photo_video_tags,user_photos,user_relationships,user_religion_politics,user_status,user_videos,user_website,user_work_history,read_friendlists,read_requests,offline_access,create_event,rsvp_event,publish_checkins,manage_pages';
function __construct()
{
$_GET['code'] = "";
parse_str($_SERVER['QUERY_STRING'], $_GET);
$ci = & get_instance();
// MAKE SURE WE HAVE PERMISSION
$fb_code = $ci->session->userdata('fb_code');
if(!$ci->session->userdata('fb_code'))
{
if( isset($_GET['code']) ){
$ci->session->set_userdata('fb_code', $_GET['code']);
}else{
redirect('https://graph.facebook.com/oauth/authorize?client_id='.$this->AppID.'&redirect;_uri='.$this->BaseUrl.'&scope;='.$this->Scope, 'refresh');
die();
}
}
}
function getToken(){
$ci = & get_instance();
$TempCode = $ci->session->userdata('fb_code');
// MAKE SURE WE HAVE ACCESS TOKEN
$ch = curl_init('https://graph.facebook.com/oauth/access_token?client_id='.$this->AppID.'&redirect;_uri='.$this->BaseUrl.'&client;_secret='.$this->Secret.'&code;='.$TempCode);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); // wait 2 miliseconds for connection
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // do not output to curl_exec()
$FbTokenFull = curl_exec($ch); // execute the cURL session
curl_close($ch);
//$FbTokenFull = @file_get_contents('https://graph.facebook.com/oauth/access_token?client_id='.$this->AppID.'&redirect;_uri='.$this->BaseUrl.'&client;_secret='.$this->Secret.'&code;='.$TempCode);
$AccessToken = str_replace('access_token=', '', $FbTokenFull);
return $AccessToken;
}
function api($Meth, $Obj, $Par=''){
// GET TOKEN
$TempToken = $this->getToken();
if(!$TempToken){
return "Sorry! Facebook's api failed.";
exit;
}
if($Meth == "post"){
$ch = curl_init();
$url = 'https://graph.facebook.com'.$Obj;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, 1);
$Attach['access_token'] = $TempToken;
if(is_array($Par)){
foreach($Par as $Key=>$Value){
$Attach[$Key] = $Value;
}
}
curl_setopt ($ch, CURLOPT_POSTFIELDS, $Attach);
}else{
$EncodePar = http_build_query($Par);
$ch = curl_init('https://graph.facebook.com'.$Obj.'?access_token='.$TempToken.'&'.$EncodePar);
echo 'https://graph.facebook.com'.$Obj.'?access_token='.$TempToken.'&'.$EncodePar;
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); // wait 2 miliseconds for connection
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // do not output to curl_exec()
}
$rsp = json_decode(curl_exec($ch)); // execute the cURL session
curl_close($ch);
return objectToArray($rsp);
}
}