Last active
April 21, 2017 23:48
-
-
Save unviva/921c1c5b3c70e67163b4c5f1198c829e to your computer and use it in GitHub Desktop.
Facebook Graph API Implementation
This file contains hidden or 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
<?php | |
/* Facebook Graph API Implementation | |
* @author Ozgur Arslan | |
* github.com/blueorange589 | |
* http://stackoverflow.com/users/7838027/Оzgur | |
* | |
* To use; | |
* 1. set configuration values | |
* 2. call public methods defined in this class | |
*/ | |
class fb { | |
// CONFIG | |
private $appid = ''; | |
private $appsecret = ''; | |
private $graphver = ''; | |
private $callbackurl = ''; | |
// PARAMS | |
private $app; | |
private $accesstoken = ''; | |
private $helper; | |
private $loginurl = ''; | |
private $permissions = ['email']; | |
private $error = ''; | |
private $oauth2; | |
private $response; | |
private $user; | |
private $object; | |
function __construct() { | |
require_once __DIR__ . '/Facebook/autoload.php'; | |
$this->app = new Facebook\Facebook([ | |
'app_id' => $this->appid, | |
'app_secret' => $this->appsecret, | |
'default_graph_version' => $this->graphver, | |
]); | |
$this->helper = $this->app->getRedirectLoginHelper(); | |
} | |
private function getloginurl() { | |
return $this->helper->getLoginUrl($this->callbackurl, $this->permissions); | |
} | |
private function getaccesstoken() { | |
if(isset($_SESSION['fb_access_token'])) { | |
$this->accesstoken = $_SESSION['fb_access_token']; | |
return $_SESSION['fb_access_token']; | |
} | |
try { | |
$this->accesstoken = $this->helper->getAccessToken(); | |
if(!$this->accesstoken) { | |
$this->debugaccesstoken(); | |
return false; | |
} | |
$this->setlonglived(); | |
$_SESSION['fb_access_token'] = $this->accesstoken; | |
// var_dump($this->accesstoken); | |
} catch(Facebook\Exceptions\FacebookResponseException $e) { | |
$this->error = $e->getMessage(); | |
exit; | |
} catch(Facebook\Exceptions\FacebookSDKException $e) { | |
$this->error = $e->getMessage(); | |
exit; | |
} | |
} | |
private function debugaccesstoken() { | |
if ($this->helper->getError()) { | |
header('HTTP/1.0 401 Unauthorized'); | |
echo "Error: " . $this->helper->getError() . "\n"; | |
echo "Error Code: " . $this->helper->getErrorCode() . "\n"; | |
echo "Error Reason: " . $this->helper->getErrorReason() . "\n"; | |
echo "Error Description: " . $this->helper->getErrorDescription() . "\n"; | |
} else { | |
header('HTTP/1.0 400 Bad Request'); | |
echo 'Bad request'; | |
} | |
} | |
private function accesstokenvalue() { | |
return $this->accesstoken->getValue(); | |
} | |
private function setoauth2() { | |
if(!$this->oauth2) { | |
$this->oauth2 = $this->app->getOAuth2Client(); | |
} | |
} | |
private function setlonglived() { | |
$this->setoauth2(); | |
if (!$this->accesstoken->isLongLived()) { | |
try { | |
$this->accesstoken = $this->oauth2->getLongLivedAccessToken($this->accesstoken); | |
} catch (Facebook\Exceptions\FacebookSDKException $e) { | |
$this->error = $helper->getMessage(); | |
exit; | |
} | |
} | |
} | |
private function request($endpoint) { | |
if(!$this->accesstoken) { | |
$this->getaccesstoken(); | |
//$this->error = "empty access token"; | |
//return false; | |
} | |
try { | |
// returns fb response object | |
$this->response = $this->app->get($endpoint, $this->accesstoken); | |
} catch(Facebook\Exceptions\FacebookResponseException $e) { | |
$this->error = $e->getMessage(); | |
exit; | |
} catch(Facebook\Exceptions\FacebookSDKException $e) { | |
$this->error = $e->getMessage(); | |
exit; | |
} | |
} | |
////------ PUBLIC --------//// | |
public function gettokenmetadata() { | |
$this->setoauth2(); | |
$this->tokenMetadata = $this->oauth2->debugToken($this->accesstoken); | |
return $this->tokenMetadata; | |
} | |
public function fetchuser($ep) { | |
$this->request($ep); | |
$this->user = $this->response->getGraphUser(); | |
return $this->user; | |
} | |
public function fetchobject($ep) { | |
$this->request($ep); | |
$this->object = $this->response->getGraphObject(); | |
return $this->object; | |
} | |
public function fetchuserpicurl() { | |
if(!$this->user['picture']) { | |
return false; | |
} | |
$pic = json_decode($this->user['picture']); | |
return $pic->url; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment