csrest_subscribers.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. require_once dirname(__FILE__).'/class/base_classes.php';
  3. /**
  4. * Class to access a subscribers resources from the create send API.
  5. * This class includes functions to add and remove subscribers ,
  6. * along with accessing statistics for a single subscriber
  7. * @author tobyb
  8. *
  9. */
  10. class CS_REST_Subscribers extends CS_REST_Wrapper_Base {
  11. /**
  12. * The base route of the subscriber resource.
  13. * @var string
  14. * @access private
  15. */
  16. var $_subscribers_base_route;
  17. /**
  18. * Constructor.
  19. * @param $list_id string The list id to access (Ignored for create requests)
  20. * @param $auth_details array Authentication details to use for API calls.
  21. * This array must take one of the following forms:
  22. * If using OAuth to authenticate:
  23. * array(
  24. * 'access_token' => 'your access token',
  25. * 'refresh_token' => 'your refresh token')
  26. *
  27. * Or if using an API key:
  28. * array('api_key' => 'your api key')
  29. * @param string $protocol The protocol to use for requests (http|https)
  30. * @param int $debug_level The level of debugging required CS_REST_LOG_NONE | CS_REST_LOG_ERROR | CS_REST_LOG_WARNING | CS_REST_LOG_VERBOSE
  31. * @param string $host The host to send API requests to. There is no need to change this
  32. * @param CS_REST_Log $log The logger to use. Used for dependency injection
  33. * @param object|null $serialiser The serialiser to use. Used for dependency injection
  34. * @param object|null $transport The transport to use. Used for dependency injection
  35. * @access public
  36. */
  37. function __construct (
  38. $list_id,
  39. $auth_details,
  40. $protocol = 'https',
  41. $debug_level = CS_REST_LOG_NONE,
  42. $host = 'api.createsend.com',
  43. $log = NULL,
  44. $serialiser = NULL,
  45. $transport = NULL) {
  46. parent::__construct($auth_details, $protocol, $debug_level, $host, $log, $serialiser, $transport);
  47. $this->set_list_id($list_id);
  48. }
  49. /**
  50. * Change the list id used for calls after construction
  51. * @param $list_id
  52. * @access public
  53. */
  54. function set_list_id($list_id) {
  55. $this->_subscribers_base_route = $this->_base_route.'subscribers/'.$list_id;
  56. }
  57. /**
  58. * Adds a new subscriber to the specified list
  59. * @param array $subscriber The subscriber details to use during creation.
  60. * This array should be of the form
  61. * array (
  62. * 'EmailAddress' => The new subscribers email address
  63. * 'Name' => The name of the new subscriber
  64. * 'CustomFields' => array(
  65. * array(
  66. * 'Key' => The custom fields personalisation tag
  67. * 'Value' => The value for this subscriber
  68. * )
  69. * )
  70. * 'Resubscribe' => Whether we should resubscribe this subscriber if they already exist in the list
  71. * 'RestartSubscriptionBasedAutoResponders' => Whether we should restart subscription based auto responders which are sent when the subscriber first subscribes to a list.
  72. * )
  73. * @access public
  74. * @return CS_REST_Wrapper_Result A successful response will be empty
  75. */
  76. function add($subscriber) {
  77. return $this->post_request($this->_subscribers_base_route.'.json', $subscriber);
  78. }
  79. /**
  80. * Updates an existing subscriber (email, name, state, or custom fields) in the specified list.
  81. * The update is performed even for inactive subscribers, but will return an error in the event of the
  82. * given email not existing in the list.
  83. * @param string $email The email address of the susbcriber to be updated
  84. * @param array $subscriber The subscriber details to use for the update. Empty parameters will remain unchanged
  85. * This array should be of the form
  86. * array (
  87. * 'EmailAddress' => The new email address
  88. * 'Name' => The name of the subscriber
  89. * 'CustomFields' => array(
  90. * array(
  91. * 'Key' => The custom fields personalisation tag
  92. * 'Value' => The value for this subscriber
  93. * 'Clear' => true/false (pass true to remove this custom field. in the case of a [multi-option, select many] field, pass an option in the 'Value' field to clear that option or leave Value blank to remove all options)
  94. * )
  95. * )
  96. * 'Resubscribe' => Whether we should resubscribe this subscriber if they already exist in the list
  97. * 'RestartSubscriptionBasedAutoResponders' => Whether we should restart subscription based auto responders which are sent when the subscriber first subscribes to a list.
  98. * )
  99. * @access public
  100. * @return CS_REST_Wrapper_Result A successful response will be empty
  101. */
  102. function update($email, $subscriber) {
  103. return $this->put_request($this->_subscribers_base_route.'.json?email='.urlencode($email), $subscriber);
  104. }
  105. /**
  106. * Imports an array of subscribers into the current list
  107. * @param array $subscribers An array of subscribers to import.
  108. * This array should be of the form
  109. * array (
  110. * array (
  111. * 'EmailAddress' => The new subscribers email address
  112. * 'Name' => The name of the new subscriber
  113. * 'CustomFields' => array(
  114. * array(
  115. * 'Key' => The custom fields personalisation tag
  116. * 'Value' => The value for this subscriber
  117. * 'Clear' => true/false (pass true to remove this custom field. in the case of a [multi-option, select many] field, pass an option in the 'Value' field to clear that option or leave Value blank to remove all options)
  118. * )
  119. * )
  120. * )
  121. * )
  122. * @param bool $resubscribe Whether we should resubscribe any existing subscribers
  123. * @param bool $queueSubscriptionBasedAutoResponders By default, subscription based auto responders do not trigger during an import. Pass a value of true to override this behaviour
  124. * @param bool $restartSubscriptionBasedAutoResponders By default, subscription based auto responders will not be restarted
  125. * @access public
  126. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  127. * {
  128. * 'TotalUniqueEmailsSubmitted' => The number of unique emails submitted in the call
  129. * 'TotalExistingSubscribers' => The number of subscribers who already existed in the list
  130. * 'TotalNewSubscribers' => The number of new subscriptions to the list
  131. * 'DuplicateEmailsInSubmission' => array<string> The emails which appeared more than once in the batch
  132. * 'FailureDetails' => array (
  133. * {
  134. * 'EmailAddress' => The email address which failed
  135. * 'Code' => The Create Send API Error code
  136. * 'Message' => The reason for the failure
  137. * }
  138. * )
  139. * }
  140. *
  141. */
  142. function import($subscribers, $resubscribe, $queueSubscriptionBasedAutoResponders = false, $restartSubscriptionBasedAutoResponders = false) {
  143. $subscribers = array(
  144. 'Resubscribe' => $resubscribe,
  145. 'QueueSubscriptionBasedAutoResponders' => $queueSubscriptionBasedAutoResponders,
  146. 'Subscribers' => $subscribers,
  147. 'RestartSubscriptionBasedAutoresponders' => $restartSubscriptionBasedAutoResponders
  148. );
  149. return $this->post_request($this->_subscribers_base_route.'/import.json', $subscribers);
  150. }
  151. /**
  152. * Gets a subscriber details, including custom fields
  153. * @access public
  154. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  155. * {
  156. * 'EmailAddress' => The subscriber email address
  157. * 'Name' => The subscribers name
  158. * 'Date' => The date the subscriber was added to the list
  159. * 'State' => The current state of the subscriber
  160. * 'CustomFields' => array(
  161. * {
  162. * 'Key' => The custom fields personalisation tag
  163. * 'Value' => The custom field value for this subscriber
  164. * }
  165. * )
  166. * }
  167. */
  168. function get($email) {
  169. return $this->get_request($this->_subscribers_base_route.'.json?email='.urlencode($email));
  170. }
  171. /**
  172. * Gets the sending history to a specific subscriber
  173. * @access public
  174. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  175. * array(
  176. * {
  177. * ID => The id of the email which was sent
  178. * Type => 'Campaign'
  179. * Name => The name of the email
  180. * Actions => array(
  181. * {
  182. * Event => The type of action (Click, Open, Unsubscribe etc)
  183. * Date => The date the event occurred
  184. * IPAddress => The IP that the event originated from
  185. * Detail => Any available details about the event i.e the URL for clicks
  186. * }
  187. * )
  188. * }
  189. * )
  190. */
  191. function get_history($email) {
  192. return $this->get_request($this->_subscribers_base_route.'/history.json?email='.urlencode($email));
  193. }
  194. /**
  195. * Unsubscribes the given subscriber from the current list
  196. * @param string $email The email address to unsubscribe
  197. * @access public
  198. * @return CS_REST_Wrapper_Result A successful response will be empty
  199. */
  200. function unsubscribe($email) {
  201. // We need to build the subscriber data structure.
  202. $email = array(
  203. 'EmailAddress' => $email
  204. );
  205. return $this->post_request($this->_subscribers_base_route.'/unsubscribe.json', $email);
  206. }
  207. /**
  208. * deletes the given subscriber from the current list
  209. * @param string $email The email address to delete
  210. * @access public
  211. * @return CS_REST_Wrapper_Result A successful response will be empty
  212. */
  213. function delete($email) {
  214. return $this->delete_request($this->_subscribers_base_route.'.json?email='.urlencode($email));
  215. }
  216. }