/lib/vendor/MRAPI/MRBase.class.php

https://github.com/webaaz/aperophp · PHP · 427 lines · 345 code · 82 blank · 0 comment · 7 complexity · e1dc7e062a2e6020b16ba0bd068d44e9 MD5 · raw file

  1. <?php
  2. class MRBase
  3. {
  4. protected $format = 'xml';
  5. protected $secure = false;
  6. protected $url;
  7. protected $timeout = 30;
  8. protected $maxRedirs = 10;
  9. protected $key;
  10. protected $methodName = '';
  11. protected $verb = 'GET';
  12. protected $requestBody = null;
  13. protected $requestLength = 0;
  14. protected $debug = false;
  15. protected $responseBody = false;
  16. protected $responseInfo = false;
  17. protected $error = null;
  18. protected $errno = null;
  19. public function __construct()
  20. {
  21. $this->url = 'api.mailingreport.com';
  22. }
  23. public function flush()
  24. {
  25. $this->requestBody = null;
  26. $this->requestLength = 0;
  27. $this->verb = 'GET';
  28. $this->methodName = null;
  29. $this->responseBody = null;
  30. $this->responseInfo = null;
  31. $this->error = null;
  32. $this->errno = null;
  33. }
  34. public function setFormat($format)
  35. {
  36. if (!in_array($format, array('json', 'xml')))
  37. {
  38. throw new Exception('The response format '.$format.' is not supported.');
  39. }
  40. $this->format = $format;
  41. }
  42. public function setVerb($verb)
  43. {
  44. if (!in_array($verb, array('GET', 'POST')))
  45. {
  46. throw new InvalidArgumentException('Current verb ('.$this->verb.') is invalid.');
  47. }
  48. $this->verb = $verb;
  49. }
  50. public function setMethodName($methodName)
  51. {
  52. $this->methodName = $methodName;
  53. }
  54. public function setKey($key)
  55. {
  56. $this->key = $key;
  57. }
  58. public function setParams(Array $params)
  59. {
  60. $this->requestBody = $params;
  61. }
  62. public function setSecure($secure)
  63. {
  64. if ($secure === true)
  65. {
  66. $this->secure = true;
  67. }
  68. else
  69. {
  70. $this->secure = false;
  71. }
  72. }
  73. public function execute()
  74. {
  75. $curlHandle = curl_init();
  76. $this->setAuth($curlHandle);
  77. try
  78. {
  79. switch(strtoupper($this->verb))
  80. {
  81. case 'GET':
  82. $this->executeGet($curlHandle);
  83. break;
  84. case 'POST':
  85. $this->executePost($curlHandle);
  86. break;
  87. case 'PUT':
  88. $this->executePut($curlHandle);
  89. break;
  90. case 'DELETE':
  91. $this->executeDelete($curlHandle);
  92. break;
  93. default:
  94. throw new InvalidArgumentException('Current verb ('.$this->verb.') is invalid.');
  95. break;
  96. }
  97. }
  98. catch(InvalidArgumentException $e)
  99. {
  100. curl_close($$curlHandlech);
  101. throw $e;
  102. }
  103. catch(Exception $e)
  104. {
  105. curl_close($curlHandle);
  106. throw $e;
  107. }
  108. return $this->responseBody;
  109. }
  110. public function buildPostBody($data = null)
  111. {
  112. $data =($data !== null) ? $data : $this->requestBody;
  113. if(!is_array($data))
  114. {
  115. throw new InvalidArgumentException('Invalid data input for parameters. Array expected.');
  116. }
  117. $data = http_build_query($data, '', '&');
  118. $this->requestBody = $data;
  119. }
  120. protected function executeGet($curlHandle)
  121. {
  122. $this->doExecute($curlHandle);
  123. }
  124. protected function executePost($curlHandle)
  125. {
  126. if(!is_string($this->requestBody))
  127. {
  128. $this->buildPostBody();
  129. }
  130. curl_setopt($curlHandle, CURLOPT_POST, true);
  131. curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $this->requestBody);
  132. $this->doExecute($curlHandle);
  133. }
  134. protected function executePut($curlHandle)
  135. {
  136. if(!is_string($this->requestBody))
  137. {
  138. $this->buildPostBody();
  139. }
  140. curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $this->requestBody);
  141. curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'PUT');
  142. $this->doExecute($curlHandle);
  143. }
  144. protected function executeDelete($curlHandle)
  145. {
  146. curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'DELETE');
  147. $this->doExecute($curlHandle);
  148. }
  149. protected function doExecute(&$curlHandle)
  150. {
  151. $this->setCurlOpts($curlHandle);
  152. $this->responseBody = curl_exec($curlHandle);
  153. $this->responseInfo = curl_getinfo($curlHandle);
  154. $this->error = curl_error($curlHandle);
  155. $this->errno = curl_errno($curlHandle);
  156. curl_close($curlHandle);
  157. }
  158. protected function setCurlOpts(&$curlHandle)
  159. {
  160. curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
  161. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
  162. curl_setopt($curlHandle, CURLOPT_MAXREDIRS, $this->timeout);
  163. curl_setopt($curlHandle, CURLOPT_MAXREDIRS, $this->maxRedirs);
  164. curl_setopt($curlHandle, CURLOPT_TIMEOUT, $this->timeout);
  165. curl_setopt($curlHandle, CURLOPT_URL, (($this->secure)?'https://':'http://').$this->url.'/'.$this->format.'/'.$this->methodName.'/');
  166. curl_setopt($curlHandle, CURLOPT_COOKIEJAR, realpath('cookie.txt'));
  167. curl_setopt($curlHandle, CURLOPT_COOKIEFILE, realpath('cookie.txt'));
  168. if ($this->secure)
  169. {
  170. curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
  171. }
  172. }
  173. protected function setAuth(&$curlHandle)
  174. {
  175. curl_setopt($curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  176. curl_setopt($curlHandle, CURLOPT_USERPWD, $this->key.':');
  177. }
  178. }
  179. class MailingReport extends MRBase
  180. {
  181. public function __construct($key)
  182. {
  183. parent::__construct();
  184. $this->setKey($key);
  185. }
  186. public function GlobalCountries()
  187. {
  188. $this->setMethodName('GlobalCountries');
  189. $this->setVerb('GET');
  190. return $this->execute();
  191. }
  192. public function GlobalLanguages()
  193. {
  194. $this->setMethodName('GlobalLanguages');
  195. $this->setVerb('GET');
  196. return $this->execute();
  197. }
  198. public function GlobalPing()
  199. {
  200. $this->setMethodName('GlobalPing');
  201. $this->setVerb('GET');
  202. return $this->execute();
  203. }
  204. public function ContactsList()
  205. {
  206. $this->setMethodName('ContactsList');
  207. $this->setVerb('GET');
  208. return $this->execute();
  209. }
  210. public function ContactsShow($email)
  211. {
  212. $this->setMethodName('ContactsShow');
  213. $this->setVerb('POST');
  214. $this->setParams(array('email' => $email));
  215. return $this->execute();
  216. }
  217. public function ContactsCreate($params)
  218. {
  219. $this->setMethodName('ContactsCreate');
  220. $this->setVerb('POST');
  221. $this->setParams($params);
  222. return $this->execute();
  223. }
  224. public function ContactsUpdate($params)
  225. {
  226. $this->setMethodName('ContactsUpdate');
  227. $this->setVerb('POST');
  228. $this->setParams($params);
  229. return $this->execute();
  230. }
  231. public function ContactsUnsubscribe($email)
  232. {
  233. $this->setMethodName('ContactsUnsubscribe');
  234. $this->setVerb('POST');
  235. $this->setParams(array('email' => $email));
  236. return $this->execute();
  237. }
  238. public function ContactsDelete($email)
  239. {
  240. $this->setMethodName('ContactsDelete');
  241. $this->setVerb('POST');
  242. $this->setParams(array('email' => $email));
  243. return $this->execute();
  244. }
  245. public function CustomFieldsList()
  246. {
  247. $this->setMethodName('CustomFieldsList');
  248. $this->setVerb('GET');
  249. return $this->execute();
  250. }
  251. public function CustomFieldsShow($apiKey)
  252. {
  253. $this->setMethodName('CustomFieldsShow');
  254. $this->setVerb('POST');
  255. $this->setParams(array('apiKey' => $apiKey));
  256. return $this->execute();
  257. }
  258. public function CustomFieldsCreate($name, $type, $isVisible)
  259. {
  260. $this->setMethodName('CustomFieldsCreate');
  261. $this->setVerb('POST');
  262. $this->setParams(array('name' => $name, 'type' => $type, 'is_visible' => $isVisible));
  263. return $this->execute();
  264. }
  265. public function CustomFieldsUpdate($apiKey, $name, $type, $isVisible)
  266. {
  267. $this->setMethodName('CustomFieldsUpdate');
  268. $this->setVerb('POST');
  269. $this->setParams(array('apiKey' => $apiKey, 'name' => $name, 'type' => $type, 'is_visible' => $isVisible));
  270. return $this->execute();
  271. }
  272. public function CustomFieldsDelete($apiKey)
  273. {
  274. $this->setMethodName('CustomFieldsDelete');
  275. $this->setVerb('POST');
  276. $this->setParams(array('apiKey' => $apiKey));
  277. return $this->execute();
  278. }
  279. public function MailingListsList()
  280. {
  281. $this->setMethodName('MailingListsList');
  282. $this->setVerb('GET');
  283. return $this->execute();
  284. }
  285. public function MailingListsShow($apiKey)
  286. {
  287. $this->setMethodName('MailingListsShow');
  288. $this->setVerb('POST');
  289. $this->setParams(array('apiKey' => $apiKey));
  290. return $this->execute();
  291. }
  292. public function MailingListsCreate($name)
  293. {
  294. $this->setMethodName('MailingListsCreate');
  295. $this->setVerb('POST');
  296. $this->setParams(array('name' => $name));
  297. return $this->execute();
  298. }
  299. public function MailingListsUpdate($apiKey, $name)
  300. {
  301. $this->setMethodName('MailingListsUpdate');
  302. $this->setVerb('POST');
  303. $this->setParams(array('apiKey' => $apiKey, 'name' => $name));
  304. return $this->execute();
  305. }
  306. public function MailingListsDelete($apiKey)
  307. {
  308. $this->setMethodName('MailingListsDelete');
  309. $this->setVerb('POST');
  310. $this->setParams(array('apiKey' => $apiKey));
  311. return $this->execute();
  312. }
  313. public function CampaignsListDraft()
  314. {
  315. $this->setMethodName('CampaignsListDraft');
  316. $this->setVerb('GET');
  317. return $this->execute();
  318. }
  319. public function CampaignsListScheduled()
  320. {
  321. $this->setMethodName('CampaignsListScheduled');
  322. $this->setVerb('GET');
  323. return $this->execute();
  324. }
  325. public function CampaignsListSent()
  326. {
  327. $this->setMethodName('CampaignsListSent');
  328. $this->setVerb('GET');
  329. return $this->execute();
  330. }
  331. public function CampaignsSummary($id)
  332. {
  333. $this->setMethodName('CampaignsSummary');
  334. $this->setVerb('POST');
  335. $this->setParams(array('id' => $id));
  336. return $this->execute();
  337. }
  338. public function AccountsShow()
  339. {
  340. $this->setMethodName('AccountsShow');
  341. $this->setVerb('GET');
  342. return $this->execute();
  343. }
  344. }
  345. ?>