Source of file SilverstripeFacebookConnector.php
Size: 3,881 Bytes - Last Modified: 2021-12-23T10:46:26+00:00
/var/www/docs.ssmods.com/process/src/src/Api/SilverstripeFacebookConnector.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 | <?php namespace SunnysideUp\ShareThis\Api; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Dev\Debug; /** * https://developers.facebook.com/tools-and-support/ */ class SilverstripeFacebookConnector { use Injectable; /** * debug * * @var boolean */ protected static $debug = false; /** * keep track of errors * @var array */ protected static $error = []; /** * @var Facebook Connection */ private static $connection = null; /** * settings for connection * * @var array */ private static $connection_config = []; /** * application ID - get from FB * * @var string */ private static $app_id = ''; /** * application secret - get from FB * * @var string */ private static $app_secret = ''; /** * set additional connection details - e.g. default_access_token * * @param array $connectionConfig */ public static function set_connection_config($connectionConfig) { self::$connection_config = $connectionConfig; } /** * @param string $openGraphCommand * * @return FacebookResponse | false */ public static function run_command($openGraphCommand = '') { $fb = self::get_connection(); $accessToken = Config::inst()->get(SilverstripeFacebookConnector::class, 'app_id') . '|' . Config::inst()->get(SilverstripeFacebookConnector::class, 'app_secret'); //$helper = $fb->getPageTabHelper(); try { $response = $fb->get($openGraphCommand, $accessToken); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error self::$error[] = 'Graph returned an error: ' . $e->getMessage(); return false; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues self::$error[] = 'Facebook SDK returned an error: ' . $e->getMessage(); return false; } if (self::$debug) { Debug::log(implode(' | ', self::$error)); } return $response; } /** * @return details about logged in person */ public static function whoami() { $response = self::run_command('/me'); if ($response) { return $response->getGraphUser(); } } /** * returns an array of recent posts for a page * * @return array */ public static function get_feed($pageID) { $response = self::run_command($pageID . '/posts?fields=message,created_time,id,full_picture,link,from,name,description'); if ($response) { $list = $response->getDecodedBody(); if (isset($list['data'])) { return $list['data']; } } } /** * returns an array of recent posts for a page * @return array */ public static function check_if_posts_exists($UID) { $response = self::run_command('/Post/' . $UID); print_r($response); } /** * create FB connection... * @return Facebook\Facebook */ protected static function get_connection() { if (! self::$connection) { self::$connection_config += [ 'app_id' => Config::inst()->get(SilverstripeFacebookConnector::class, 'app_id'), 'app_secret' => Config::inst()->get(SilverstripeFacebookConnector::class, 'app_secret'), 'default_graph_version' => 'v2.4', //'default_access_token' => '{access-token}', // optional ]; self::$connection = new Facebook\Facebook(self::$connection_config); } return self::$connection; } } |