Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseluisq/b98516bc1da6f6153614 to your computer and use it in GitHub Desktop.
Save joseluisq/b98516bc1da6f6153614 to your computer and use it in GitHub Desktop.
PHP Class for Facebook API handle
<?php
/**
* Class for Ajax request and response data structure
* @author Jose Luis Quintana <joseluisquintana20@gmail.com>
*/
class FacebookCustom {
private $env;
private $app_id;
private $app_secret;
private $app_scope;
private $app_uri;
private $app_fb_uri;
private $facebook;
/**
*
* @param type $app_id
* @param type $app_secret
* @param type $app_uri
* @param type $app_scope
* @param type $env
*/
function __construct($app_id = '', $app_secret = '', $app_fb_uri = '', $app_uri = '', $app_scope = 'email', $env = 'development') {
$this->env = $env;
$this->app_id = $app_id;
$this->app_secret = $app_secret;
$this->app_scope = $app_scope;
$this->app_fb_uri = $app_fb_uri;
$this->app_uri = $app_uri;
}
/**
* Initialize the Facebook library
*/
function initialize() {
require_once 'facebook/Facebook.php';
$this->facebook = new Facebook(array(
'appId' => $this->app_id,
'secret' => $this->app_secret,
'cookie' => TRUE,
'grant_type' => 'client_credentials'
));
}
/**
* Set the environment for application
* @param string $env 'development' || 'production'
*/
function setEnvironment($env = 'development') {
$this->env = $env;
}
/**
* Set the app facebook id
* @param string $app_id
*/
function setAppId($app_id) {
$this->app_id = $app_id;
}
/**
* Set the app facebook secret
* @param string $app_secret
*/
function setAppSecret($app_secret) {
$this->app_secret = $app_secret;
}
/**
* Set the app facebook scope
* @param string $app_scope For examle: email, publish_stream, etc
*/
function setAppScope($app_scope = 'email') {
$this->app_scope = $app_scope;
}
/**
* Set the app facebook url
* @param string $app_fb_uri
*/
function setAppFbUri($app_fb_uri) {
$this->app_fb_uri = $app_fb_uri;
}
/**
* Set the app url
* @param string $app_uri
*/
function setAppUri($app_uri) {
$this->app_uri = $app_uri;
}
/**
* Gets the Facebook class
* @return Facebook
*/
function getSDK() {
return $this->facebook;
}
/**
* Gets user facebook profile
* @return Array
*/
function getProfile() {
$user_profile = NULL;
if ($this->env === 'development') {
$user_profile = array(
'id' => '223230109915511',
'email' => 'john.doe@company.com',
'first_name' => 'John',
'gender' => 'male',
'last_name' => 'Doe',
'link' => 'https://www.facebook.com/app_scoped_user_id/223230109915511/',
'locale' => 'en_US',
'name' => 'John Doe',
'timezone' => -5,
'updated_time' => '2014-06-04T03:19:28+0000',
'verified' => 1
);
} else {
$user = $this->facebook->getUser();
if ($user) {
$user_profile = $this->facebook->api('/me');
}
}
return $user_profile;
}
/**
* Redirect for authentication UI.
* @param string $redirection_mode 'iframe' || 'window'
*/
function auth($redirection_mode = 'iframe') {
$login_url = $this->getLoginUrl();
$level = ($redirection_mode === 'iframe') ? '.top' : '';
echo "Espere por favor.."
. "<script type='text/javascript'>(function(w){w{$level}.location.href='{$login_url}';})(window);</script>";
exit;
}
/**
* Gets Facebook Auth url
* @return string
*/
function getLoginUrl() {
$login_url = $this->facebook->getLoginUrl(array(
'scope' => $this->app_scope,
'redirect_uri' => $this->app_uri
));
return $login_url;
}
/**
* Send facebook notification
* @param type $url
* @param type $user_id
* @return Array
*/
function sendNotification($user_id, $url, $template) {
$post = $this->api('/' . $user_id . '/notifications/', 'post', array(
'access_token' => $this->app_id . '|' . $this->app_secret,
'href' => $url,
'template' => $template,
'ref' => 'Notificación enviada a las ' . date("Y-m-d G:i:s") //this is for Facebook's insight
));
return $post;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment