/urbanairship.php

https://github.com/midnightskinhead/php-library · PHP · 217 lines · 173 code · 29 blank · 15 comment · 43 complexity · 7fb40b9851c15a6f1a5a0903718d30f3 MD5 · raw file

  1. <?php
  2. // Php module for using the Urban Airship API
  3. require_once 'RESTClient.php';
  4. define('SERVER', 'go.urbanairship.com');
  5. define('BASE_URL', 'https://go.urbanairship.com/api');
  6. define('DEVICE_TOKEN_URL', BASE_URL . '/device_tokens/');
  7. define('PUSH_URL', BASE_URL . '/push/');
  8. define('BROADCAST_URL', BASE_URL . '/push/broadcast/');
  9. define('FEEDBACK_URL', BASE_URL . '/device_tokens/feedback/');
  10. // Raise when we get a 401 from the server.
  11. class Unauthorized extends Exception {
  12. }
  13. // Raise when we get an error response from the server.
  14. // args are (status code, message).
  15. class AirshipFailure extends Exception {
  16. }
  17. class AirshipDeviceList implements Iterator, Countable {
  18. private $_airship = null;
  19. private $_page = null;
  20. private $_position = 0;
  21. public function __construct($airship) {
  22. $this->_airship = $airship;
  23. $this->_load_page(DEVICE_TOKEN_URL);
  24. $this->_position = 0;
  25. }
  26. private function _load_page($url) {
  27. $response = $this->_airship->_request($url, 'GET', null, null);
  28. $response_code = $response[0];
  29. if ($response_code != 200) {
  30. throw new AirshipFailure($response[1], $response_code);
  31. }
  32. $result = json_decode($response[1]);
  33. var_dump($result->next_page);
  34. if ($this->_page == null) {
  35. $this->_page = $result;
  36. } else {
  37. echo 'got next page of device tokens';
  38. $this->_page->device_tokens = array_merge($this->_page->device_tokens, $result->device_tokens);
  39. $this->_page->next_page = $result->next_page;
  40. }
  41. }
  42. // Countable interface
  43. public function count() {
  44. return $this->_page->device_tokens_count;
  45. }
  46. // Iterator interface
  47. function rewind() {
  48. $this->_position = 0;
  49. }
  50. function current() {
  51. return $this->_page->device_tokens[$this->_position];
  52. }
  53. function key() {
  54. return $this->_position;
  55. }
  56. function next() {
  57. ++$this->_position;
  58. }
  59. function valid() {
  60. if (!isset($this->_page->device_tokens[$this->_position])) {
  61. $next_page = $this->_page->next_page;
  62. if ($next_page == null) {
  63. return false;
  64. } else {
  65. $this->_load_page($next_page);
  66. return $this->valid();
  67. }
  68. }
  69. return true;
  70. }
  71. }
  72. class Airship {
  73. private $key = '';
  74. private $secret = '';
  75. public function __construct($key, $secret) {
  76. $this->key = $key;
  77. $this->secret = $secret;
  78. return true;
  79. }
  80. public function _request($url, $method, $body, $content_type=null) {
  81. $rest = new RESTClient($this->key, $this->secret, $content_type);
  82. $rest->createRequest($url, $method, $body);
  83. $rest->sendRequest();
  84. $response = $rest->getResponse();
  85. if ($response[0] == 401) {
  86. throw new Unauthorized();
  87. }
  88. return $response;
  89. }
  90. // Register the device token with UA.
  91. public function register($device_token, $alias=null, $tags=null, $badge=null) {
  92. $url = DEVICE_TOKEN_URL . $device_token;
  93. $payload = array();
  94. if ($alias != null) {
  95. $payload['alias'] = $alias;
  96. }
  97. if ($tags != null) {
  98. $payload['tags'] = $tags;
  99. }
  100. if ($badge != null) {
  101. $payload['badge'] = $badge;
  102. }
  103. if (count($payload) != 0) {
  104. $body = json_encode($payload);
  105. $content_type = 'application/json';
  106. } else {
  107. $body = '';
  108. $content_type = null;
  109. }
  110. $response = $this->_request($url, 'PUT', $body, $content_type);
  111. $response_code = $response[0];
  112. if ($response_code != 201 && $response_code != 200) {
  113. throw new AirshipFailure($response[1], $response_code);
  114. }
  115. return ($response_code == 201);
  116. }
  117. // Mark the device token as inactive.
  118. public function deregister($device_token) {
  119. $url = DEVICE_TOKEN_URL . $device_token;
  120. $response = $this->_request($url, 'DELETE', null, null);
  121. $response_code = $response[0];
  122. if ($response_code != 204) {
  123. throw new AirshipFailure($response[1], $response_code);
  124. }
  125. }
  126. // Retrieve information about this device token.
  127. public function get_device_token_info($device_token) {
  128. $url = DEVICE_TOKEN_URL . $device_token;
  129. $response = $this->_request($url, 'GET', null, null);
  130. $response_code = $response[0];
  131. if ($response_code != 200) {
  132. throw new AirshipFailure($response[1], $response_code);
  133. }
  134. return json_decode($response[1]);
  135. }
  136. public function get_device_tokens() {
  137. return new AirshipDeviceList($this);
  138. }
  139. // Push this payload to the specified device tokens and tags.
  140. public function push($payload, $device_tokens=null, $aliases=null, $tags=null) {
  141. if ($device_tokens != null) {
  142. $payload['device_tokens'] = $device_tokens;
  143. }
  144. if ($aliases != null) {
  145. $payload['aliases'] = $aliases;
  146. }
  147. if ($tags != null) {
  148. $payload['tags'] = $tags;
  149. }
  150. $body = json_encode($payload);
  151. $response = $this->_request(PUSH_URL, 'POST', $body, 'application/json');
  152. $response_code = $response[0];
  153. if ($response_code != 200) {
  154. throw new AirshipFailure($response[1], $response_code);
  155. }
  156. }
  157. // Broadcast this payload to all users.
  158. public function broadcast($payload, $exclude_tokens=null) {
  159. if ($exclude_tokens != null) {
  160. $payload['exclude_tokens'] = $exclude_tokens;
  161. }
  162. $body = json_encode($payload);
  163. $response = $this->_request(BROADCAST_URL, 'POST', $body, 'application/json');
  164. $response_code = $response[0];
  165. if ($response_code != 200) {
  166. throw new AirshipFailure($response[1], $response_code);
  167. }
  168. }
  169. /*
  170. Return device tokens marked as inactive since this timestamp
  171. Return a list of (device token, timestamp, alias) functions.
  172. */
  173. public function feedback($since) {
  174. $url = FEEDBACK_URL . '?' . 'since=' . rawurlencode($since->format('c'));
  175. $response = $this->_request($url, 'GET', null, null);
  176. $response_code = $response[0];
  177. if ($response_code != 200) {
  178. throw new AirshipFailure($response[1], $response_code);
  179. }
  180. $results = json_decode($response[1]);
  181. foreach ($results as $item) {
  182. $item->marked_inactive_on = new DateTime($item->marked_inactive_on,
  183. new DateTimeZone('UTC'));
  184. }
  185. return $results;
  186. }
  187. }
  188. ?>