Created
January 6, 2014 10:00
-
-
Save jonathantneal/8280607 to your computer and use it in GitHub Desktop.
Simple Facebook PHP API
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
<?php | |
class FacebookBase { | |
public static $curlOpts = array( | |
CURLOPT_CONNECTTIMEOUT => 10, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_TIMEOUT => 60, | |
CURLOPT_USERAGENT => 'facebook-php', | |
); | |
public function curl($url = '', $get = array(), $post = array()) { | |
$opts = self::$curlOpts; | |
if (!empty($get)) $url .= '?'.http_build_query($get); | |
if (!empty($post)) $opts[CURLOPT_POSTFIELDS] = http_build_query($post, '', '&', PHP_QUERY_RFC3986); | |
$opts[CURLOPT_URL] = $url; | |
$ch = curl_init(); | |
curl_setopt_array($ch, $opts); | |
$content = curl_exec($ch); | |
curl_close($ch); | |
return $content; | |
} | |
public function graph($url = '', $getParams = array(), $postParams = array()) { | |
$response = $this->curl('https://graph.facebook.com/'.$url, $getParams, $postParams); | |
$responseJSON = json_decode($response, false); | |
parse_str($response, $responseParam); | |
return (object) ( | |
!empty($responseJSON) ? $responseJSON : ( | |
!empty($responseParam) ? $responseParam : array( | |
'content' => $response | |
) | |
) | |
); | |
} | |
public function currentURL() { | |
$isHTTPS = $_SERVER['HTTPS'] === 'on'; | |
$isPorted = $_SERVER['SERVER_PORT'] !== ($isHTTPS ? '443' : '80'); | |
$protocol = $isHTTPS ? 'https' : 'http'; | |
$port = $isPorted ? ':'.$_SERVER['SERVER_PORT'] : ''; | |
return $protocol.'://'.$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; | |
} | |
public function has($var = '') { | |
return !empty($this->{$var}); | |
} | |
} | |
class Facebook extends FacebookBase { | |
private $appID; | |
private $appSecret; | |
private $authCode; | |
private $authToken; | |
private $accounts = array(); | |
private $authTokens = array(); | |
public function __construct($options = array(), $save = false) { | |
$this->authCode = $_GET['code']; | |
$this->authToken = $_GET['access_token']; | |
foreach ($options as $key => $value) $this->$key = $value; | |
} | |
public function initAuthCode() { | |
if (empty($this->authCode)) { | |
header( | |
'Location: '. | |
sprintf( | |
'https://www.facebook.com/dialog/oauth?scope=%s&response_type=%s&client_id=%s&redirect_uri=%s', | |
'manage_pages,publish_stream', | |
'code', | |
$this->appID, | |
$this->currentURL() | |
) | |
); | |
} | |
} | |
public function initAuthToken() { | |
if (empty($this->authToken)) { | |
$response = $this->graph('oauth/access_token', array( | |
'client_id' => $this->appID, | |
'client_secret' => $this->appSecret, | |
'grant_type' => 'client_credentials' | |
)); | |
if (!$response->error) $this->authToken = $response->access_token; | |
return $response; | |
} | |
} | |
public function initActiveAuthToken() { | |
if (empty($this->authToken)) { | |
$response = $this->graph('oauth/access_token', array( | |
'client_id' => $this->appID, | |
'client_secret' => $this->appSecret, | |
'code' => $this->authCode, | |
'redirect_uri' => $this->currentURL() | |
)); | |
if (!$response->error) $this->authToken = $response->access_token; | |
return $response; | |
} | |
} | |
public function initProfile() { | |
$response = $this->graph('me', array( | |
'access_token' => $this->authToken | |
)); | |
if (!$response->error) { | |
$this->profile = $response; | |
$this->authTokens[$response->id] = $this->authToken; | |
} | |
return $response; | |
} | |
public function initAccounts() { | |
$response = $this->graph('me/accounts', array( | |
'access_token' => $this->authToken | |
)); | |
if (!$response->error) { | |
foreach ($response->data as &$account) { | |
$this->authTokens[$account->id] = $account->access_token; | |
unset($account->access_token); | |
$this->accounts[$account->id] = $account; | |
} | |
} | |
return $response; | |
} | |
public function feed($id = '', $maximum = 10) { | |
if (!empty($id)) { | |
return $this->graph($id.'/feed', array( | |
'access_token' => $this->authToken, | |
'limit' => $maximum | |
)); | |
} | |
} | |
public function post($id = '', $options = array()) { | |
if ($id === 'profile') $id = $this->profile->id; | |
$authToken = empty($this->authTokens[$id]) ? $this->authToken : $this->authTokens[$id]; | |
return $this->graph($id . '/feed', null, array_merge(array( | |
'access_token' => $authToken | |
), $options)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You haven't mentioned anything like what these functions do.. whether to use our access token or not.. if you mentioned something that will be helpful