/php/src/ReportGrid.php

https://github.com/germc/client-libraries · PHP · 325 lines · 148 code · 74 blank · 103 comment · 22 complexity · 3662b65754689c372e103a58e8e6b26b MD5 · raw file

  1. <?php
  2. /**
  3. * Provides access to the Report Grid API platform.
  4. *
  5. **/
  6. /*
  7. * PUT LICENSE HERE
  8. */
  9. define ("API_VERSION", "v0");
  10. define ("MAX_TIME", "9223372036854775807");
  11. define ("BASE_URL", "http://api.reportgrid.com/services/analytics/");
  12. class ReportGridAPI {
  13. private $_tokenID = null;
  14. public $isError = false;
  15. public $errorMessage = null;
  16. /*
  17. * Initialize a new ReportGridAPI object
  18. *
  19. * @param String $token_id
  20. *
  21. */
  22. public function __construct($token_id) {
  23. $this->_tokenID = $token_id;
  24. }
  25. /*
  26. * Create a new token
  27. *
  28. * @param String path The path, relative to the parent's path, that will be associated with this tokenId
  29. * @param String expires The expiration date of the token, measured in milliseconds from the start of the Unix Epoch, UTC time
  30. * @param String read Does this token have read permissions
  31. * @param String write Does this token have write permissions
  32. * @param String share Does this token have share permissions
  33. * @param String order The maximum number of sets in an intersection query
  34. * @param String limit The maximum number of properties associated with an events
  35. * @param String depth The maximum depth of properties associated with events
  36. *
  37. * @return String token
  38. */
  39. public function newToken($path = "", $expires = MAX_TIME, $read = false, $write = false, $share = false, $order = 0, $limit = 0, $depth = 0) {
  40. $return_value = null;
  41. $params = array();
  42. $params['path'] = $path;
  43. $perms = array();
  44. $perms['read'] = $read;
  45. $perms['write'] = $write;
  46. $perms['share'] = $share;
  47. $params['permissions'] = $perms;
  48. $params['expires'] = $expires;
  49. $limits = array();
  50. $limits['order'] = $order;
  51. $limits['limit'] = $limit;
  52. $limits['depth'] = $depth;
  53. $params['limits'] = $limits;
  54. $result = $this->restHelper(BASE_URL . API_VERSION . "/tokens/?tokenId=" . $this->_tokenID, $params, "POST");
  55. if (isset($result[0])) {
  56. $return_value = $result[0];
  57. }
  58. return $return_value;
  59. }
  60. /*
  61. * Return all tokens this->_tokenId is a parent of
  62. *
  63. * @returns Array - All tokens associated with this->_tokenId
  64. */
  65. public function getTokens() {
  66. $return_value = null;
  67. $return_value = $this->restHelper(BASE_URL . API_VERSION . "/tokens/?tokenId=" . $this->_tokenID, null, "GET");
  68. return $return_value;
  69. }
  70. /*
  71. * todo: doc
  72. * Dis B BUSTED
  73. */
  74. public function updateToken($token="", $path = "", $expires = MAX_TIME, $read = false, $write = false, $share = false, $order = 0, $limit = 0, $depth = 0) {
  75. $return_value = null;
  76. $params = array();
  77. $params['path'] = $path;
  78. $perms = array();
  79. $perms['read'] = $read;
  80. $perms['write'] = $write;
  81. $perms['share'] = $share;
  82. $params['permissions'] = $perms;
  83. $params['expires'] = $expires;
  84. $limits = array();
  85. $limits['order'] = $order;
  86. $limits['limit'] = $limit;
  87. $limits['depth'] = $depth;
  88. $params['limits'] = $limits;
  89. $return_value = $this->restHelper(BASE_URL . API_VERSION . "/tokens/" . $token . "?tokenId=" . $this->_tokenID, $params, "PUT");
  90. return $return_value;
  91. }
  92. /*
  93. * Return an array of data about a specific token
  94. *
  95. * @param String - Token
  96. *
  97. * @return Array - All information about this token
  98. */
  99. public function tokenInfo($token) {
  100. $return_value = null;
  101. $return_value = $this->restHelper(BASE_URL . API_VERSION . "/tokens/" . $token . "?tokenId=" . $this->_tokenID, null, "GET");
  102. return $return_value;
  103. }
  104. /*
  105. * Delete an existing token
  106. *
  107. * @param String - Token
  108. *
  109. * @return int - 0/1. 0=fail 1=success
  110. */
  111. public function deleteToken($token) {
  112. $return_value = null;
  113. $return_value = $this->restHelper(BASE_URL . API_VERSION . "/tokens/" . $token . "?tokenId=" . $this->_tokenID, null, "DELETE");
  114. return $return_value;
  115. }
  116. /*
  117. * Record a new event
  118. *
  119. * @param String - path
  120. * @param Array - event data
  121. *
  122. * @return int - 0/1. 0=fail 1=success
  123. */
  124. public function recordEvent($path = "", $params = array()) {
  125. $return_value = false;
  126. $return_value = $this->restHelper(BASE_URL . API_VERSION . "/vfs/" . $path . "/?tokenId=" . $this->_tokenID, $params, "POST");
  127. return $return_value;
  128. }
  129. /*
  130. * Retrieve an event
  131. *
  132. * @param String - path
  133. * @param String - interaction
  134. * @param String - type
  135. * @param String - periodicity
  136. *
  137. * @return Array - event occurances
  138. */
  139. public function retrieveEvent($path = "", $interaction = "", $type = "", $periodicity = "eternity") {
  140. $return_value = null;
  141. if ($periodicity != "count") {
  142. $periodicity = "series/" . $periodicity;
  143. }
  144. $url = BASE_URL . API_VERSION . "/vfs/" . $path . "/" . $interaction . "/values/" . $type . "/" . $periodicity . "?tokenId=" . $this->_tokenID;
  145. $result = $this->restHelper($url, null, "GET", "json");
  146. if ($result) {
  147. $return_value = $result;
  148. }
  149. return $return_value;
  150. }
  151. /*
  152. * Delete an existing event
  153. *
  154. * @param String - path
  155. * @param Array - event parameters
  156. *
  157. * @return int - 0/1. 0=fail 1=success
  158. */
  159. public function deleteEvents($path = "", $params = array()) {
  160. $return_value = false;
  161. $return_value = $this->restHelper(BASE_URL . API_VERSION . "/vfs/" . $path . "/?tokenId=" . $this->_tokenID, $params, "POST");
  162. return $return_value;
  163. }
  164. /*
  165. * Search events
  166. *
  167. * @param String - select
  168. * @param String - from
  169. * @param Array - event parameters
  170. *
  171. * @return Array - search results
  172. */
  173. function search($select = "", $from = "", $where = array()) {
  174. $return_value = null;
  175. $params = array();
  176. $params['select'] = $select;
  177. $params['from'] = $from;
  178. $params['where'] = $where;
  179. $result = $this->restHelper(BASE_URL . API_VERSION . "/search?tokenId=" . $this->_tokenID, $params, "POST");
  180. if ($result) {
  181. $return_value = $result;
  182. }
  183. return $return_value;
  184. }
  185. /****************************************************************************/
  186. /****************************************************************************/
  187. /*********************************
  188. **** PRIVATE helper function ****
  189. *********************************/
  190. private function restHelper($json_endpoint, $params = null, $verb = 'GET') {
  191. $return_value = null;
  192. $http_params = array(
  193. 'http' => array(
  194. 'method' => $verb,
  195. 'ignore_errors' => false
  196. ));
  197. if ($params !== null) {
  198. if ( ($verb == 'POST') || ($verb == 'PUT') ) {
  199. $http_params['http']['content'] = json_encode($params);
  200. $http_params['http']['header'] = array("Content-Type: application/json");
  201. }//end if
  202. }//end if ($params !== null)
  203. $stream_context = stream_context_create($http_params);
  204. $file_pointer = fopen($json_endpoint, 'rb', false, $stream_context);
  205. if (!$file_pointer) {
  206. $stream_contents = false;
  207. } else {
  208. $stream_meta_data = stream_get_meta_data($file_pointer);
  209. $stream_contents = stream_get_contents($file_pointer);
  210. }
  211. if ($stream_contents !== false) {
  212. /*
  213. * In the case of we're receiving stream data back from the API,
  214. * json decode it here.
  215. */
  216. if (strlen($stream_contents) > 0) {
  217. $result = (array)json_decode($stream_contents);
  218. if ($result === null) {
  219. error_log("Exception: " . $stream_contents);
  220. } else {
  221. $return_value = $result;
  222. }
  223. /*
  224. * In the case of posting data (recordEvent) the API will return a 0
  225. * length response, in this scenario we're looking for the http 200
  226. * header code to indicate the data was successfully received.
  227. */
  228. } else {
  229. if (stripos($stream_meta_data['wrapper_data'][0], "200") !== false) {
  230. $return_value = true;
  231. } else {
  232. $return_value = false;
  233. }//end inner else
  234. }//end middle else
  235. } else {
  236. /*
  237. * If there's an error message in the response
  238. * headers...send that back to the user
  239. */
  240. if (isset($http_response_header[0])) {
  241. $this->isError = true;
  242. $this->errorMessage = $http_response_header[0];
  243. $return_value = false;
  244. } else {
  245. throw new Exception("$verb $json_endpoint failed");
  246. }
  247. }//end outer else
  248. return $return_value;
  249. }//end restHelper
  250. }
  251. ?>