From b4e98fe7411fe2c584805782aeddebba63c590da Mon Sep 17 00:00:00 2001 From: Michael Pitidis Date: Fri, 13 Sep 2013 17:38:22 +0100 Subject: [PATCH 1/2] Add support for creating and deleting content with the Facebook Graph API --- .../DataSift/Storyplayer/Prose/FromCurl.php | 28 +++++++++++--- .../Prose/UsingFacebookGraphApi.php | 37 ++++++++++++++++--- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/php/DataSift/Storyplayer/Prose/FromCurl.php b/src/php/DataSift/Storyplayer/Prose/FromCurl.php index 313f55d8..40c71f82 100644 --- a/src/php/DataSift/Storyplayer/Prose/FromCurl.php +++ b/src/php/DataSift/Storyplayer/Prose/FromCurl.php @@ -35,7 +35,7 @@ * * @category Libraries * @package Storyplayer/Prose - * @author Michael Heap + * @author Michael Heap , Michael Pitidis * @copyright 2011-present Mediasift Ltd www.datasift.com * @license http://www.opensource.org/licenses/bsd-license.php BSD License * @link http://datasift.github.io/storyplayer @@ -72,21 +72,36 @@ class FromCurl extends Prose */ public function get($url, $params = array(), $headers = array()) { + return $this->request($url, $params, $headers); + } + + public function post($url, $data, $params = array(), $headers = array()) + { + return $this->request($url, $params, $headers, 'POST', array( + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $data, + )); + } + + public function delete($url, $params = array(), $headers = array()) + { + return $this->request($url, $params, $headers, 'DELETE', array( + CURLOPT_CUSTOMREQUEST => "DELETE", + )); + } + protected function request($url, $params = array(), $headers = array(), $method = 'GET', $options = array()) { if (count($headers)){ throw new E5xx_NotImplemented("FromCurl does not support headers yet"); } - // shorthand - $st = $this->st; - // create the full URL if (count($params) > 0) { $url = $url . '?' . http_build_query($params); } // what are we doing? - $log = $st->startAction("HTTP GET '${url}'"); + $log = $this->st->startAction("HTTP $method '${url}'"); // create a new cURL resource $ch = curl_init(); @@ -95,6 +110,9 @@ public function get($url, $params = array(), $headers = array()) curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); + foreach ($options as $key => $val) { + curl_setopt($ch, $key, $val); + } // grab URL and pass it to the browser $response = curl_exec($ch); diff --git a/src/php/DataSift/Storyplayer/Prose/UsingFacebookGraphApi.php b/src/php/DataSift/Storyplayer/Prose/UsingFacebookGraphApi.php index 1e071108..2f7efa5e 100644 --- a/src/php/DataSift/Storyplayer/Prose/UsingFacebookGraphApi.php +++ b/src/php/DataSift/Storyplayer/Prose/UsingFacebookGraphApi.php @@ -35,7 +35,7 @@ * * @category Libraries * @package Storyplayer/Prose - * @author Michael Heap + * @author Michael Heap , Michael Pitidis * @copyright 2011-present Mediasift Ltd www.datasift.com * @license http://www.opensource.org/licenses/bsd-license.php BSD License * @link http://datasift.github.io/storyplayer @@ -97,6 +97,28 @@ public function getLatestPostFromPage($id){ return reset($posts); } + public function getPostUpdatedTime($post_id) { + return $this->st->fromCurl()->get("{$this->base_path}/{$post_id}", $this->authenticated(array( + 'fields' => 'updated_time', + ))); + } + + public function createComment($post_id, $comment) { + return $this->makeGraphPostRequest("/{$post_id}/comments", "message=$comment"); + } + + public function deleteComment($comment_id) { + return $this->makeGraphDeleteRequest("/{$comment_id}"); + } + + private function makeGraphPostRequest($path, $data, $params = array()) { + return $this->st->fromCurl()->post($this->base_path . $path, $data, $this->authenticated($params)); + } + + private function makeGraphDeleteRequest($path, $params = array()) { + return $this->st->fromCurl()->delete($this->base_path . $path, $this->authenticated($params)); + } + /** * makeGraphApiRequest * @@ -106,14 +128,12 @@ public function getLatestPostFromPage($id){ * * @return void */ - private function makeGraphApiRequest($path){ + private function makeGraphApiRequest($path, $params = array()){ $st = $this->st; $environment = $st->getEnvironment(); - $access_token = $environment->facebookAccessToken; - // GET $path/?access_token=$access_token - $resp = $st->fromCurl()->get($this->base_path.$path.'?access_token='. $access_token); + $resp = $st->fromCurl()->get($this->base_path . $path, $this->authenticated($params)); // Make sure it's well formed $log = $st->startAction("make sure we have the 'data' key in the response"); @@ -136,4 +156,11 @@ private function makeGraphApiRequest($path){ return $resp->data; } + + private function authenticated(array $params) { + if (!isset($params['access_token'])) { + $params['access_token'] = $this->st->getEnvironment()->facebookAccessToken; + } + return $params; + } } From bdd4cf7f3ba05eb5064e016776429d056847987d Mon Sep 17 00:00:00 2001 From: Michael Pitidis Date: Thu, 17 Oct 2013 16:04:26 +0100 Subject: [PATCH 2/2] Rename `authenticated` to `addAccessToken` --- .../Storyplayer/Prose/UsingFacebookGraphApi.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/php/DataSift/Storyplayer/Prose/UsingFacebookGraphApi.php b/src/php/DataSift/Storyplayer/Prose/UsingFacebookGraphApi.php index 2f7efa5e..7f16b94f 100644 --- a/src/php/DataSift/Storyplayer/Prose/UsingFacebookGraphApi.php +++ b/src/php/DataSift/Storyplayer/Prose/UsingFacebookGraphApi.php @@ -98,7 +98,7 @@ public function getLatestPostFromPage($id){ } public function getPostUpdatedTime($post_id) { - return $this->st->fromCurl()->get("{$this->base_path}/{$post_id}", $this->authenticated(array( + return $this->st->fromCurl()->get("{$this->base_path}/{$post_id}", $this->addAccessToken(array( 'fields' => 'updated_time', ))); } @@ -112,11 +112,11 @@ public function deleteComment($comment_id) { } private function makeGraphPostRequest($path, $data, $params = array()) { - return $this->st->fromCurl()->post($this->base_path . $path, $data, $this->authenticated($params)); + return $this->st->fromCurl()->post($this->base_path . $path, $data, $this->addAccessToken($params)); } private function makeGraphDeleteRequest($path, $params = array()) { - return $this->st->fromCurl()->delete($this->base_path . $path, $this->authenticated($params)); + return $this->st->fromCurl()->delete($this->base_path . $path, $this->addAccessToken($params)); } /** @@ -133,7 +133,7 @@ private function makeGraphApiRequest($path, $params = array()){ $environment = $st->getEnvironment(); - $resp = $st->fromCurl()->get($this->base_path . $path, $this->authenticated($params)); + $resp = $st->fromCurl()->get($this->base_path . $path, $this->addAccessToken($params)); // Make sure it's well formed $log = $st->startAction("make sure we have the 'data' key in the response"); @@ -157,7 +157,7 @@ private function makeGraphApiRequest($path, $params = array()){ return $resp->data; } - private function authenticated(array $params) { + private function addAccessToken(array $params) { if (!isset($params['access_token'])) { $params['access_token'] = $this->st->getEnvironment()->facebookAccessToken; }