Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Add support for creating and deleting content with the Facebook Graph AP... #68

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/php/DataSift/Storyplayer/Prose/FromCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @category Libraries
* @package Storyplayer/Prose
* @author Michael Heap <[email protected]>
* @author Michael Heap <[email protected]>, Michael Pitidis <[email protected]>
* @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
Expand Down Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be moved to usingCurl. fromCurl should only be for idempotent methods.

{
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();
Expand All @@ -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);
Expand Down
37 changes: 32 additions & 5 deletions src/php/DataSift/Storyplayer/Prose/UsingFacebookGraphApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @category Libraries
* @package Storyplayer/Prose
* @author Michael Heap <[email protected]>
* @author Michael Heap <[email protected]>, Michael Pitidis <[email protected]>
* @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
Expand Down Expand Up @@ -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
*
Expand All @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to rename $this->authenticated to $this->addAccessToken


// Make sure it's well formed
$log = $st->startAction("make sure we have the 'data' key in the response");
Expand All @@ -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;
}
}