/impermium.php

https://github.com/PopSugar/Impermium-PHP · PHP · 449 lines · 136 code · 49 blank · 264 comment · 8 complexity · b9ad1821805d0329a0a82782c00d831a MD5 · raw file

  1. <?php
  2. /**
  3. * Impermium-PHP
  4. *
  5. * A simple PHP wrapper for Impermium
  6. *
  7. * Usage:
  8. *
  9. * $impermium = new ImpermiumAPI($api_key);
  10. *
  11. * $response = $impermium->contentComment($event_id, array(
  12. * 'uid_ref' => 'foo',
  13. * 'content' => 'bar',
  14. * 'resource_url' => 'http://www.popsugar.com/',
  15. * 'enduser_ip' => '127.0.0.1',
  16. * ));
  17. *
  18. * if ($response->spam->label == 'spam') {
  19. * echo "It's spam!";
  20. * }
  21. *
  22. * @author Alec Vallintine <avallintine@sugarinc.com>
  23. * @version 0.1
  24. * @copyright Copyright (c) 2011 Sugar Inc.
  25. * @link https://github.com/PopSugar/Impermium-PHP
  26. * @license see LICENSE
  27. */
  28. if (!function_exists('curl_init')) {
  29. throw new Exception('Impermium-PHP needs the CURL PHP extension.');
  30. }
  31. if (!function_exists('json_decode')) {
  32. throw new Exception('Impermium-PHP needs the JSON PHP extension.');
  33. }
  34. class ImpermiumAPIException extends Exception {
  35. }
  36. /**
  37. * @throws ImpermiumAPIException
  38. *
  39. */
  40. class ImpermiumAPI {
  41. /**
  42. * Version
  43. */
  44. const VERSION = '0.1';
  45. /**
  46. * Default Impermium API version
  47. */
  48. const API_VERSION = '2.0';
  49. /**
  50. * Default Impermium API host name
  51. */
  52. const API_HOST = 'api-test.impermium.com';
  53. /**
  54. * The API key issued to you when you signed up for the service
  55. *
  56. * @var string
  57. * @access private
  58. */
  59. private $_apiKey;
  60. /**
  61. * Impermium API host name
  62. *
  63. * @var string
  64. * @access private
  65. */
  66. private $_apiHost;
  67. /**
  68. * Impermium API version
  69. *
  70. * @var string
  71. * @access private
  72. */
  73. private $_apiVersion;
  74. /**
  75. * Default options for curl
  76. *
  77. * @var array
  78. * @access private
  79. */
  80. private $_curlOpts = array(
  81. CURLOPT_RETURNTRANSFER => true,
  82. CURLOPT_POST => true,
  83. CURLOPT_CONNECTTIMEOUT => 10,
  84. CURLOPT_TIMEOUT => 60,
  85. CURLOPT_USERAGENT => 'Impermium-PHP/0.1',
  86. CURLOPT_HTTPHEADER => array(
  87. 'Content-Type: application/json',
  88. ),
  89. );
  90. /**
  91. * Constructor
  92. *
  93. * @param string $api_key
  94. * @param string $api_host [optional]
  95. * @param string $api_version [optional]
  96. * @access public
  97. */
  98. public function __construct($api_key, $api_host=self::API_HOST, $api_version=self::API_VERSION) {
  99. $this->_apiKey = $api_key;
  100. $this->_apiHost = $api_host;
  101. $this->_apiVersion = $api_version;
  102. }
  103. /**
  104. * Builds a request url
  105. *
  106. * @param string $type
  107. * @param string $action
  108. * @param string|int $event_id
  109. * @return string
  110. * @access private
  111. */
  112. private function _buildUrl($type, $action, $event_id) {
  113. $url = sprintf('http://%s/%s/%s/%s/%s/%s', $this->_apiHost, $type, $action, $this->_apiVersion, $this->_apiKey, $event_id);
  114. return $url;
  115. }
  116. /**
  117. * Makes a request to Impermium and returns the decoded response
  118. *
  119. * @param string $type
  120. * @param string $action
  121. * @param string|int $event_id
  122. * @param array $params
  123. * @return object
  124. * @access private
  125. * @throws ImpermiumAPIException
  126. */
  127. private function _request($type, $action, $event_id, $params) {
  128. $url = $this->_buildUrl($type, $action, $event_id);
  129. $post_data = json_encode($params);
  130. $ch = curl_init();
  131. curl_setopt_array($ch, $this->_curlOpts);
  132. curl_setopt($ch, CURLOPT_URL, $url);
  133. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  134. $response = curl_exec($ch);
  135. $error = false;
  136. if ($response === false) {
  137. $error = curl_error($ch);
  138. }
  139. else {
  140. $response_data = json_decode($response);
  141. }
  142. if (!$error && curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
  143. $error = $response_data->message;
  144. }
  145. if ($error) {
  146. curl_close($ch);
  147. throw new ImpermiumAPIException($error);
  148. }
  149. curl_close($ch);
  150. return $response_data;
  151. }
  152. /**
  153. * Gets the API key
  154. *
  155. * @return string
  156. * @access public
  157. */
  158. public function getApiKey() {
  159. return $this->_apiKey;
  160. }
  161. /**
  162. * Sets the API key
  163. *
  164. * @param string $api_key
  165. * @return void
  166. * @access public
  167. */
  168. public function setApiKey($api_key) {
  169. $this->_apiKey = $api_key;
  170. }
  171. /**
  172. * Gets the API host
  173. *
  174. * @return string
  175. * @access public
  176. */
  177. public function getApiHost() {
  178. return $this->_apiHost;
  179. }
  180. /**
  181. * Sets the API host
  182. *
  183. * @param string $api_host
  184. * @return void
  185. * @access public
  186. */
  187. public function setApiHost($api_host) {
  188. $this->_apiHost = $api_host;
  189. }
  190. /**
  191. * Gets the API version
  192. *
  193. * @return string
  194. * @access public
  195. */
  196. public function getApiVersion() {
  197. return $this->_apiVersion;
  198. }
  199. /**
  200. * Sets the API version
  201. *
  202. * @param string $api_version
  203. * @return void
  204. * @access public
  205. */
  206. public function setApiVersion($api_version) {
  207. $this->_apiVersion = $api_version;
  208. }
  209. /**
  210. * Gets the options for curl
  211. *
  212. * @return array
  213. * @access public
  214. */
  215. public function getCurlOpts() {
  216. return $this->_curlOpts;
  217. }
  218. /**
  219. * Sets the options for curl
  220. *
  221. * @param array $curl_opts
  222. * @return void
  223. * @access public
  224. */
  225. public function setCurlOpts($curl_opts) {
  226. $this->_curlOpts = $curl_opts;
  227. }
  228. /**
  229. * @param string|int $event_id
  230. * @param array $params
  231. * @return object
  232. * @access public
  233. * @link http://impermium.com/api
  234. */
  235. public function accountLogin($event_id, $params) {
  236. return $this->_request('account', 'login', $event_id, $params);
  237. }
  238. /**
  239. * @param string|int $event_id
  240. * @param array $params
  241. * @return object
  242. * @access public
  243. * @link http://impermium.com/api
  244. */
  245. public function accountProfile($event_id, $params) {
  246. return $this->_request('account', 'profile', $event_id, $params);
  247. }
  248. /**
  249. * @param string|int $event_id
  250. * @param array $params
  251. * @return object
  252. * @access public
  253. * @link http://impermium.com/api
  254. */
  255. public function accountSignup($event_id, $params) {
  256. return $this->_request('account', 'signup', $event_id, $params);
  257. }
  258. /**
  259. * @param string|int $event_id
  260. * @param array $params
  261. * @return object
  262. * @access public
  263. * @link http://impermium.com/api
  264. */
  265. public function accountSignupAttempt($event_id, $params) {
  266. return $this->_request('account', 'signup_attempt', $event_id, $params);
  267. }
  268. /**
  269. * @param string|int $event_id
  270. * @param array $params
  271. * @return object
  272. * @access public
  273. * @link http://impermium.com/api
  274. */
  275. public function connectionInvite($event_id, $params) {
  276. return $this->_request('connection', 'invite', $event_id, $params);
  277. }
  278. /**
  279. * @param string|int $event_id
  280. * @param array $params
  281. * @return object
  282. * @access public
  283. * @link http://impermium.com/api
  284. */
  285. public function connectionInviteResponse($event_id, $params) {
  286. return $this->_request('connection', 'invite_response', $event_id, $params);
  287. }
  288. /**
  289. * @param string|int $event_id
  290. * @param array $params
  291. * @return object
  292. * @access public
  293. * @link http://impermium.com/api
  294. */
  295. public function contentBlogEntry($event_id, $params) {
  296. return $this->_request('content', 'blog_entry', $event_id, $params);
  297. }
  298. /**
  299. * @param string|int $event_id
  300. * @param array $params
  301. * @return object
  302. * @access public
  303. * @link http://impermium.com/api
  304. */
  305. public function contentChatMessage($event_id, $params) {
  306. return $this->_request('content', 'chat_message', $event_id, $params);
  307. }
  308. /**
  309. * @param string|int $event_id
  310. * @param array $params
  311. * @return object
  312. * @access public
  313. * @link http://impermium.com/api
  314. */
  315. public function contentChatroomMessage($event_id, $params) {
  316. return $this->_request('content', 'chatroom_message', $event_id, $params);
  317. }
  318. /**
  319. * @param string|int $event_id
  320. * @param array $params
  321. * @return object
  322. * @access public
  323. * @link http://impermium.com/api
  324. */
  325. public function contentComment($event_id, $params) {
  326. return $this->_request('content', 'comment', $event_id, $params);
  327. }
  328. /**
  329. * @param string|int $event_id
  330. * @param array $params
  331. * @return object
  332. * @access public
  333. * @link http://impermium.com/api
  334. */
  335. public function contentForumMessage($event_id, $params) {
  336. return $this->_request('content', 'forum_message', $event_id, $params);
  337. }
  338. /**
  339. * @param string|int $event_id
  340. * @param array $params
  341. * @return object
  342. * @access public
  343. * @link http://impermium.com/api
  344. */
  345. public function contentGeneric($event_id, $params) {
  346. return $this->_request('content', 'generic', $event_id, $params);
  347. }
  348. /**
  349. *
  350. *
  351. * @param string|int $event_id
  352. * @param array $params
  353. * @return object
  354. * @access public
  355. * @link http://impermium.com/api
  356. */
  357. public function contentMessage($event_id, $params) {
  358. return $this->_request('content', 'message', $event_id, $params);
  359. }
  360. /**
  361. * @param string|int $event_id
  362. * @param array $params
  363. * @return object
  364. * @access public
  365. * @link http://impermium.com/api
  366. */
  367. public function contentRating($event_id, $params) {
  368. return $this->_request('content', 'rating', $event_id, $params);
  369. }
  370. /**
  371. * @param string|int $event_id
  372. * @param array $params
  373. * @return object
  374. * @access public
  375. * @link http://impermium.com/api
  376. */
  377. public function contentWallMessage($event_id, $params) {
  378. return $this->_request('content', 'wall_message', $event_id, $params);
  379. }
  380. /**
  381. * @param string|int $event_id
  382. * @param array $params
  383. * @return object
  384. * @access public
  385. * @link http://impermium.com/api
  386. */
  387. public function feedbackContent($event_id, $params) {
  388. return $this->_request('feedback', 'content', $event_id, $params);
  389. }
  390. /**
  391. * @param string|int $event_id
  392. * @param array $params
  393. * @return object
  394. * @access public
  395. * @link http://impermium.com/api
  396. */
  397. public function feedbackUser($event_id, $params) {
  398. return $this->_request('feedback', 'user', $event_id, $params);
  399. }
  400. }