SendyPHP.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace SendyPHP;
  3. /**
  4. * Sendy Class
  5. */
  6. class SendyPHP
  7. {
  8. protected $installation_url;
  9. protected $api_key;
  10. protected $list_id;
  11. public function __construct(array $config)
  12. {
  13. //error checking
  14. $list_id = @$config['list_id'];
  15. $installation_url = @$config['installation_url'];
  16. $api_key = @$config['api_key'];
  17. if (empty($list_id)) {
  18. throw new \Exception("Required config parameter [list_id] is not set or empty", 1);
  19. }
  20. if (empty($installation_url)) {
  21. throw new \Exception("Required config parameter [installation_url] is not set or empty", 1);
  22. }
  23. if (empty($api_key)) {
  24. throw new \Exception("Required config parameter [api_key] is not set or empty", 1);
  25. }
  26. $this->list_id = $list_id;
  27. $this->installation_url = $installation_url;
  28. $this->api_key = $api_key;
  29. }
  30. public function setListId($list_id)
  31. {
  32. if (empty($list_id)) {
  33. throw new \Exception("Required config parameter [list_id] is not set", 1);
  34. }
  35. $this->list_id = $list_id;
  36. }
  37. public function getListId()
  38. {
  39. return $this->list_id;
  40. }
  41. public function subscribe(array $values)
  42. {
  43. $type = 'subscribe';
  44. //Send the subscribe
  45. $result = strval($this->buildAndSend($type, $values));
  46. //Handle results
  47. switch ($result) {
  48. case '1':
  49. return array(
  50. 'status' => true,
  51. 'message' => 'Subscribed'
  52. );
  53. break;
  54. case 'Already subscribed.':
  55. return array(
  56. 'status' => true,
  57. 'message' => 'Already subscribed.'
  58. );
  59. break;
  60. default:
  61. return array(
  62. 'status' => false,
  63. 'message' => $result
  64. );
  65. break;
  66. }
  67. }
  68. public function unsubscribe($email)
  69. {
  70. $type = 'unsubscribe';
  71. //Send the unsubscribe
  72. $result = strval($this->buildAndSend($type, array('email' => $email)));
  73. //Handle results
  74. switch ($result) {
  75. case '1':
  76. return array(
  77. 'status' => true,
  78. 'message' => 'Unsubscribed'
  79. );
  80. break;
  81. default:
  82. return array(
  83. 'status' => false,
  84. 'message' => $result
  85. );
  86. break;
  87. }
  88. }
  89. public function substatus($email)
  90. {
  91. $type = 'api/subscribers/subscription-status.php';
  92. //Send the request for status
  93. $result = $this->buildAndSend($type, array(
  94. 'email' => $email,
  95. 'api_key' => $this->api_key,
  96. 'list_id' => $this->list_id
  97. ));
  98. //Handle the results
  99. switch ($result) {
  100. case 'Subscribed':
  101. case 'Unsubscribed':
  102. case 'Unconfirmed':
  103. case 'Bounced':
  104. case 'Soft bounced':
  105. case 'Complained':
  106. return array(
  107. 'status' => true,
  108. 'message' => $result
  109. );
  110. break;
  111. default:
  112. return array(
  113. 'status' => false,
  114. 'message' => $result
  115. );
  116. break;
  117. }
  118. }
  119. public function subcount($list = "")
  120. {
  121. $type = 'api/subscribers/active-subscriber-count.php';
  122. //if a list is passed in use it, otherwise use $this->list_id
  123. if (empty($list)) {
  124. $list = $this->list_id;
  125. }
  126. //handle exceptions
  127. if (empty($list)) {
  128. throw new \Exception("method [subcount] requires parameter [list] or [$this->list_id] to be set.", 1);
  129. }
  130. //Send request for subcount
  131. $result = $this->buildAndSend($type, array(
  132. 'api_key' => $this->api_key,
  133. 'list_id' => $list
  134. ));
  135. //Handle the results
  136. if (is_numeric($result)) {
  137. return array(
  138. 'status' => true,
  139. 'message' => $result
  140. );
  141. }
  142. //Error
  143. return array(
  144. 'status' => false,
  145. 'message' => $result
  146. );
  147. }
  148. private function buildAndSend($type, array $values)
  149. {
  150. //error checking
  151. if (empty($type)) {
  152. throw new \Exception("Required config parameter [type] is not set or empty", 1);
  153. }
  154. if (empty($values)) {
  155. throw new \Exception("Required config parameter [values] is not set or empty", 1);
  156. }
  157. //Global options for return
  158. $return_options = array(
  159. 'list' => $this->list_id,
  160. 'boolean' => 'true'
  161. );
  162. //Merge the passed in values with the options for return
  163. $content = array_merge($values, $return_options);
  164. //build a query using the $content
  165. $postdata = http_build_query($content);
  166. $ch = curl_init($this->installation_url .'/'. $type);
  167. // Settings to disable SSL verification for testing (leave commented for production use)
  168. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  169. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  170. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  171. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  172. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  173. curl_setopt($ch, CURLOPT_POST, 1);
  174. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  175. $result = curl_exec($ch);
  176. curl_close($ch);
  177. return $result;
  178. }
  179. }