* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBStream'); /** * Facebook connector * * @version 0.1.0 * @package WB * @subpackage base */ class WBFacebook { /** * configuration * @var WBConfig */ private $conf; /** * Graph base URL */ const FB_GRAPH_BASE_URL = 'https://graph.facebook.com'; /** * current facebook access token * @var string */ private $token = ''; /** * facebook application id * @var string */ private $appId; /** * stream context * @var array */ private $streamContextOption; /** * constructor * * Load facebook config and get stream context options */ public function __construct($params = array()) { $this->conf = WBClass::create('WBConfig'); $this->conf->load('facebook'); $this->appId = $this->conf->get('account/appid'); $this->streamContextOption = WBStream::getStreamContextOptions(self::FB_GRAPH_BASE_URL); } /** * publish a wall post * * @see http://developers.facebook.com/docs/reference/api/post/ * * @param array $data * @param array %actions * @return string $id */ public function postOnWall($data = array(), $actions = array()) { if (empty($data) || !is_array($data) || !isset($data['message']) || empty($data['message'])) { WBClass::load('WBException_Argument'); throw new WBException_Argument('Invalid argument. $data must be an associative array with "message" set', 1, __CLASS__); } $keys = array( 'message', 'link', 'picture', 'name', 'caption', 'description', 'privacy', 'targeting' ); $post = array(); foreach ($keys as $key) { if (!isset($data[$key]) || empty($data[$key])) { continue; } $post[$key] = $data[$key]; } // actions if (!empty($actions)) { $post['actions'] = json_encode($actions); } $this->fetchAccessToken(); $url = sprintf('%s/%s/feed' , self::FB_GRAPH_BASE_URL , $this->appId); $res = $this->request($url, $data); if (is_array($res) && isset($res['id'])) { return $res['id']; } return null; } /** * fetch access token for further requests * * Use appid and appsecret to create session like access token */ private function fetchAccessToken() { if (!empty($this->token)) { return; } $url = sprintf('%s/oauth/access_token?client_id=%s&client_secret=%s&grant_type=client_credentials' , self::FB_GRAPH_BASE_URL , $this->appId , $this->conf->get('account/appsecret') ); $token = trim($this->request($url, array(), true)); $token = explode('=', $token); $this->token = $token[1]; } /** * Send GET or POST request to facebook * * Auxillary method to build facebook requests. Send request and receive response. * * @param string $url * @param array $post optional post data * @param bool $raw whether to decode JSON string (default) or return raw data * @return array */ private function request($url, $post = array(), $raw = false) { $option = $this->streamContextOption; if (!empty($post)) { $post['access_token'] = $this->token; $option['http']['method'] = 'POST'; $option['http']['header'] = 'Content-type: application/x-www-form-urlencoded'; $option['http']['content'] = http_build_query($post); } $context = stream_context_create($option); $res = file_get_contents($url, false, $context); if (false === $res) { WBClass::load('WBException_Net'); throw new WBException_Net('Facebook request failed.', 2, __CLASS__); } if ($raw) { return $res; } if (!strlen($res)) { return array(); } return json_decode($res, true); } } ?>