Skip to content

Instantly share code, notes, and snippets.

@nyeholt
Created August 4, 2014 00:32
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 nyeholt/725f550cc83d6e46551c to your computer and use it in GitHub Desktop.
Save nyeholt/725f550cc83d6e46551c to your computer and use it in GitHub Desktop.
<?php
/**
* @author Marcus Nyeholt <marcus@silverstripe.com.au>
*/
class FacebookService {
static $api_urls = array(
'object' => 'https://graph.facebook.com/%s',
'auth' => 'https://graph.facebook.com/oauth/access_token?client_id=%s&client_secret=%s&grant_type=client_credentials',
'feed' => 'https://graph.facebook.com/%s/feed',
'albums' => 'https://graph.facebook.com/%s/albums',
'photos' => 'https://graph.facebook.com/%s/photos',
);
static $app_id;
static $app_secret;
static $default_id;
private $cache;
public function webEnabledMethods() {
return array(
'getPhotos' => 'GET',
'getObject' => 'GET',
);
}
public function publicWebMethods() {
return array(
'getPhotos' => true,
'getObject' => true
);
}
public function __construct() {
$this->cache = SimpleCache::get_cache();
}
/**
*
* @param type $id
* @param type $limit
*/
public function getFeed($id = null, $limit = 0, $mine = null) {
if (!$id) {
$id = self::$default_id;
}
$token = $this->getAuthToken();
$key = 'feed-' . $id . '-' . $limit;
if ($token && !($data = $this->cache->get($key))) {
$url = sprintf(self::$api_urls['feed'], $id) . '?access_token=' . $token;
if ($limit) {
$url .= '&limit=' . $limit;
}
$data = file_get_contents($url);
if ($data) {
$this->cache->store($key, $data, 1800);
}
}
if (function_exists('json_decode')) {
$data = json_decode($data);
} else {
$data = Convert::json2obj($data);
}
$result = new ArrayList();
if ($data && $data->data) {
foreach ($data->data as $item) {
if ($mine) {
if ($item->from && $item->from->id != $mine) {
continue;
}
}
$objType = 'FacebookStatus';
if ($item->type && $item->type == 'photo') {
$item->Photo = new FacebookPhoto($this->getObject($item->object_id));
}
$result->push(new $objType($item));
}
return $result;
}
}
/**
*
* @param type $id
* @param type $limit
*/
public function getPhotos($id = null, $limit = 0, $mine = null) {
if (!$id) {
$id = self::$default_id;
}
$token = $this->getAuthToken();
$key = 'photos-' . $id . '-' . $limit;
if ($token && !($data = $this->cache->get($key))) {
$url = sprintf(self::$api_urls['photos'], $id) . '?access_token=' . $token;
if ($limit) {
$url .= '&limit=' . $limit;
}
$data = file_get_contents($url);
if ($data) {
$this->cache->store($key, $data, 1800);
}
}
if (function_exists('json_decode')) {
$data = json_decode($data);
} else {
$data = Convert::json2obj($data);
}
$result = new ArrayList();
if ($data && $data->data) {
foreach ($data->data as $item) {
if ($mine) {
if ($item->from && $item->from->id != $mine) {
continue;
}
}
$objType = 'FacebookPhoto';
$result->push(new $objType($item));
}
return $result;
}
}
public function getObject($id, $cacheLength = 3600) {
$key = 'object-' . $id;
$token = $this->getAuthToken();
if ($token && !($data = $this->cache->get($key))) {
$url = sprintf(self::$api_urls['object'], $id) . '?access_token=' . $token;
$data = file_get_contents($url);
if ($data) {
$this->cache->store($key, $data, $cacheLength);
}
}
if (function_exists('json_decode')) {
$data = json_decode($data);
} else {
$data = Convert::json2obj($data);
}
return $data;
}
protected function getAuthToken() {
$token = Session::get('facebook_token');
if (!$token) {
$content = file_get_contents(sprintf(self::$api_urls['auth'], self::$app_id, self::$app_secret));
if ($content && strpos($content, '=')) {
list($junk, $token) = explode('=', $content);
Session::set('facebook_token', $token);
}
}
return $token;
}
}
class FacebookObject extends ViewableData {
protected $data;
public function __construct($data) {
$this->data = $data;
}
public function toMap() {
return $this->data;
}
public function __get($field) {
if (isset($this->data->$field)) {
return $this->data->$field;
}
}
public function NiceTime($field, $format = 'jS F') {
return date($format, strtotime($this->$field));
}
}
class FacebookPhoto extends FacebookObject {
public function getThumb($index=3) {
$imgs = $this->images;
for ($i = $index; $i >= 0; $i--) {
if (isset($imgs[$i])) {
return $imgs[$i]->source;
}
}
}
}
class FacebookStatus extends FacebookObject {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment