Created
November 26, 2011 16:46
-
-
Save NikoRoberts/1395961 to your computer and use it in GitHub Desktop.
Manybots API PHP example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* | |
Manybots API example | |
Author: Niko Roberts (www.nikoroberts.com) | |
Date: 25.11.11 | |
Description: goes to https://www.manybots.com/ and get a list of your latest phone activities | |
*/ | |
header('Content-type: text/html; charset=UTF-8'); | |
include "MyCurl.php"; //class file is required | |
$TOKEN = 'YOURAPIKEY'; | |
class Client { | |
protected $token, $mc, $base_uri; | |
public function __construct($t) { | |
$this->token = $t; | |
$this->base_uri = 'https://www.manybots.com'; | |
$this->mc = new MyCurl(); //create an instance | |
$this->mc->getContent = true; | |
$this->mc->getHeaders = false; | |
} | |
public function filter_activities($filters) { | |
$mb_area = '/activities/filter.json'; //the target json from Manybots | |
$http_code = ''; | |
$arr_headers = array(); | |
$url = $this->base_uri . $mb_area; | |
// $auth = '?auth_token='.$this->token; //the auth token in query form | |
// $query = $auth . '&' . http_build_query($filters); //build the query from the auth and filters | |
// $url = $this->base_uri . $mb_area . $query; | |
return json_decode($this->mc->post( $url, $filters ,$arr_headers,$http_code), true); //get all data from the compiled URL and make into a PHP associative array | |
} | |
public function create_activity($activity) { | |
$mb_area = '/activities.json'; //the target json from Manybots | |
$url = $this->base_uri . $mb_area; | |
$http_code = ''; | |
$arr_headers = array(); | |
return json_decode($this->mc->post($url, $activity, $arr_headers,$http_code), true); //get all data from the compiled URL and make into a PHP associative array | |
} | |
} | |
//create the Manybots client | |
$myClient = new Client($TOKEN); | |
//get the activities from Manybots and filter using the filters (see more on https://gist.github.com/webcracy) | |
$activities = $myClient->filter_activities( | |
//the filters that we want to use | |
array( | |
"auth_token" => $TOKEN, | |
"filter" => array ( | |
'verbs' => 'demo' | |
) | |
) | |
); | |
// format the activities and display the information we want to see | |
foreach($activities['data']['items'] as $activity) { | |
echo $activity['title']. '<br>'; | |
} | |
echo "<br>Finished showing existing activities. Now, creating a new one.<br><br>"; | |
$myClient1 = new Client($TOKEN); | |
$result = $myClient1->create_activity( | |
array( | |
"auth_token" => $TOKEN, | |
"version" => '1.0', | |
"activity" => array( | |
"id" => 'http://demoapp.com/1', | |
"url" => 'http://demoapp.com/1', | |
"published" => date('Y-m-d\TH:i:s.uP'), | |
"actor" => array( | |
"id" => 'http://nikoroberts.com', | |
"url" => 'http://nikoroberts.com', | |
"displayName" => 'Niko Roberts', | |
), | |
"verb" => 'demo', | |
"object" => array( | |
"objectType" => 'company', | |
"id" => 'http://www.tasboa.com', | |
"url" => 'http://www.tasboa.com', | |
"displayName" => 'Tasboa' | |
), | |
"target" => array( | |
"objectType" => 'person', | |
"id" => 'http://webcracy.org', | |
"url" => 'http://webcracy.org', | |
"displayName" => 'Alex Solleiro' | |
), | |
"tags" => array('test', 'code', 'php', 'api'), | |
"auto_title" => true, | |
"title" => 'ACTOR demoed OBJECT to TARGET', | |
"summary" => '', | |
"content" => '', | |
"icon" => 'http://www.verticalliftbsi.com/images/cloud-icon.png', //32x32 icon for the activity | |
"generator" => array( | |
"url" => 'https://www.myapp.com/activity_generator', | |
"id" => 'http://www.myapp.com/activity_generator', | |
"displayName" => 'The PHP Activity Generator', | |
"image" => array( | |
"url" => '' //32x32 icon for the activity generator | |
) | |
), | |
"provider" => array( | |
"url" => 'https://gist.github.com/gists/1395961', | |
"id" => 'https://gist.github.com/gists/1395961', | |
"displayName" => 'Manybots PHP API Demo', | |
"image" => array( | |
"url" => '' //32x32 icon for the provider | |
) | |
) | |
) | |
) | |
); | |
echo $result['title']; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* | |
Class: MyCurl | |
Author: Skakunov Alex ([email protected]) | |
Date: 26.11.06 | |
Description: provides a simple tool to GET/POST data with help of CURL library | |
* | |
* Modification History: | |
* 30-Nov-2011 , Nicholas Roberts | |
* - Changed compile_post_data function to just use http_build_query instead of custom (broken) version | |
*/ | |
class MyCurl | |
{ | |
public $getHeaders = true;//headers will be added to output | |
public $getContent = true; //contens will be added to output | |
public $followRedirects = true; //should the class go to another URL, if the current is "HTTP/1.1 302 Moved Temporarily" | |
private $fCookieFile; | |
private $fSocket; | |
function MyCurl() | |
{ | |
$this->fCookieFile = tempnam("/tmp", "g_"); | |
} | |
function init() | |
{ | |
return $this->fSocket = curl_init(); | |
} | |
function setopt($opt, $value) | |
{ | |
return curl_setopt($this->fSocket, $opt, $value); | |
} | |
function load_defaults() | |
{ | |
$this->setopt(CURLOPT_RETURNTRANSFER, 1); | |
$this->setopt(CURLOPT_FOLLOWLOCATION, $this->followRedirects); | |
$this->setopt(CURLOPT_REFERER, "http://google.com"); | |
$this->setopt(CURLOPT_VERBOSE, false); | |
$this->setopt(CURLOPT_SSL_VERIFYPEER, false); | |
$this->setopt(CURLOPT_SSL_VERIFYHOST, false); | |
$this->setopt(CURLOPT_HEADER, $this->getHeaders); | |
$this->setopt(CURLOPT_NOBODY, !$this->getContent); | |
$this->setopt(CURLOPT_COOKIEJAR, $this->fCookieFile); | |
$this->setopt(CURLOPT_COOKIEFILE, $this->fCookieFile); | |
$this->setopt(CURLOPT_USERAGENT, "MyCurl"); | |
$this->setopt(CURLOPT_POST, 1); | |
$this->setopt(CURLOPT_CUSTOMREQUEST,'POST'); | |
} | |
function destroy() | |
{ | |
return curl_close($this->fSocket); | |
} | |
function head($url) | |
{ | |
$this->init(); | |
if($this->fSocket) | |
{ | |
$this->getHeaders = true; | |
$this->getContent = false; | |
$this->load_defaults(); | |
$this->setopt(CURLOPT_POST, 0); | |
$this->setopt(CURLOPT_CUSTOMREQUEST,'HEAD'); | |
$this->setopt(CURLOPT_URL, $url); | |
$result = curl_exec($this->fSocket); | |
$this->destroy(); | |
return $result; | |
} | |
return 0; | |
} | |
function get($url) | |
{ | |
$this->init(); | |
if($this->fSocket) | |
{ | |
$this->load_defaults(); | |
$this->setopt(CURLOPT_POST, 0); | |
$this->setopt(CURLOPT_CUSTOMREQUEST,'GET'); | |
$this->setopt(CURLOPT_URL, $url); | |
$result = curl_exec($this->fSocket); | |
$this->destroy(); | |
return $result; | |
} | |
return 0; | |
} | |
function post($url, $post_data, $arr_headers, &$http_code) | |
{ | |
$this->init(); | |
if($this->fSocket) | |
{ | |
$post_data = $this->compile_post_data($post_data); | |
$this->load_defaults(); | |
if(!empty($post_data)) | |
$this->setopt(CURLOPT_POSTFIELDS, $post_data); | |
if($arr_headers=='') { $arr_headers = array(); } | |
if(!empty($arr_headers)) | |
$this->setopt(CURLOPT_HTTPHEADER, $arr_headers); | |
$this->setopt(CURLOPT_URL, $url); | |
$result = curl_exec($this->fSocket); | |
$http_code = curl_getinfo($this->fSocket, CURLINFO_HTTP_CODE); | |
$this->destroy(); | |
return $result; | |
} | |
return 0; | |
} | |
function compile_post_data($post_data) | |
{ | |
$result = ''; | |
if(!empty($post_data)) | |
$result = http_build_query($post_data); | |
return $result; | |
} | |
function get_parsed($result, $bef, $aft="") | |
{ | |
$line=1; | |
$len = strlen($bef); | |
$pos_bef = strpos($result, $bef); | |
if($pos_bef===false) | |
return ""; | |
$pos_bef+=$len; | |
if(empty($aft)) | |
{ //try to search up to the end of line | |
$pos_aft = strpos($result, "\n", $pos_bef); | |
if($pos_aft===false) | |
$pos_aft = strpos($result, "\r\n", $pos_bef); | |
} | |
else | |
$pos_aft = strpos($result, $aft, $pos_bef); | |
if($pos_aft!==false) | |
$rez = substr($result, $pos_bef, $pos_aft-$pos_bef); | |
else | |
$rez = substr($result, $pos_bef); | |
return $rez; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment