M2_Mailchimp

The Membership 2 M2 Mailchimp class.

Defined (1)

The class is defined in the following location(s).

/lib/mailchimp-api/Mailchimp.php  
  1. class M2_Mailchimp { 
  2.  
  3. public $apikey; 
  4. public $ch; 
  5. public $root = 'https://api.mailchimp.com/2.0'; 
  6. public $debug = false; 
  7.  
  8. public static $error_map = array( 
  9. "ValidationError" => "M2_Mailchimp_ValidationError",  
  10. "ServerError_MethodUnknown" => "M2_Mailchimp_ServerError_MethodUnknown",  
  11. "ServerError_InvalidParameters" => "M2_Mailchimp_ServerError_InvalidParameters",  
  12. "Unknown_Exception" => "M2_Mailchimp_Unknown_Exception",  
  13. "Request_TimedOut" => "M2_Mailchimp_Request_TimedOut",  
  14. "Zend_Uri_Exception" => "M2_Mailchimp_Zend_Uri_Exception",  
  15. "PDOException" => "M2_Mailchimp_PDOException",  
  16. "Avesta_Db_Exception" => "M2_Mailchimp_Avesta_Db_Exception",  
  17. "XML_RPC2_Exception" => "M2_Mailchimp_XML_RPC2_Exception",  
  18. "XML_RPC2_FaultException" => "M2_Mailchimp_XML_RPC2_FaultException",  
  19. "Too_Many_Connections" => "M2_Mailchimp_Too_Many_Connections",  
  20. "Parse_Exception" => "M2_Mailchimp_Parse_Exception",  
  21. "User_Unknown" => "M2_Mailchimp_User_Unknown",  
  22. "User_Disabled" => "M2_Mailchimp_User_Disabled",  
  23. "User_DoesNotExist" => "M2_Mailchimp_User_DoesNotExist",  
  24. "User_NotApproved" => "M2_Mailchimp_User_NotApproved",  
  25. "Invalid_ApiKey" => "M2_Mailchimp_Invalid_ApiKey",  
  26. "User_UnderMaintenance" => "M2_Mailchimp_User_UnderMaintenance",  
  27. "Invalid_AppKey" => "M2_Mailchimp_Invalid_AppKey",  
  28. "Invalid_IP" => "M2_Mailchimp_Invalid_IP",  
  29. "User_DoesExist" => "M2_Mailchimp_User_DoesExist",  
  30. "User_InvalidRole" => "M2_Mailchimp_User_InvalidRole",  
  31. "User_InvalidAction" => "M2_Mailchimp_User_InvalidAction",  
  32. "User_MissingEmail" => "M2_Mailchimp_User_MissingEmail",  
  33. "User_CannotSendCampaign" => "M2_Mailchimp_User_CannotSendCampaign",  
  34. "User_MissingModuleOutbox" => "M2_Mailchimp_User_MissingModuleOutbox",  
  35. "User_ModuleAlreadyPurchased" => "M2_Mailchimp_User_ModuleAlreadyPurchased",  
  36. "User_ModuleNotPurchased" => "M2_Mailchimp_User_ModuleNotPurchased",  
  37. "User_NotEnoughCredit" => "M2_Mailchimp_User_NotEnoughCredit",  
  38. "MC_InvalidPayment" => "M2_Mailchimp_MC_InvalidPayment",  
  39. "List_DoesNotExist" => "M2_Mailchimp_List_DoesNotExist",  
  40. "List_InvalidInterestFieldType" => "M2_Mailchimp_List_InvalidInterestFieldType",  
  41. "List_InvalidOption" => "M2_Mailchimp_List_InvalidOption",  
  42. "List_InvalidUnsubMember" => "M2_Mailchimp_List_InvalidUnsubMember",  
  43. "List_InvalidBounceMember" => "M2_Mailchimp_List_InvalidBounceMember",  
  44. "List_AlreadySubscribed" => "M2_Mailchimp_List_AlreadySubscribed",  
  45. "List_NotSubscribed" => "M2_Mailchimp_List_NotSubscribed",  
  46. "List_InvalidImport" => "M2_Mailchimp_List_InvalidImport",  
  47. "MC_PastedList_Duplicate" => "M2_Mailchimp_MC_PastedList_Duplicate",  
  48. "MC_PastedList_InvalidImport" => "M2_Mailchimp_MC_PastedList_InvalidImport",  
  49. "Email_AlreadySubscribed" => "M2_Mailchimp_Email_AlreadySubscribed",  
  50. "Email_AlreadyUnsubscribed" => "M2_Mailchimp_Email_AlreadyUnsubscribed",  
  51. "Email_NotExists" => "M2_Mailchimp_Email_NotExists",  
  52. "Email_NotSubscribed" => "M2_Mailchimp_Email_NotSubscribed",  
  53. "List_MergeFieldRequired" => "M2_Mailchimp_List_MergeFieldRequired",  
  54. "List_CannotRemoveEmailMerge" => "M2_Mailchimp_List_CannotRemoveEmailMerge",  
  55. "List_Merge_InvalidMergeID" => "M2_Mailchimp_List_Merge_InvalidMergeID",  
  56. "List_TooManyMergeFields" => "M2_Mailchimp_List_TooManyMergeFields",  
  57. "List_InvalidMergeField" => "M2_Mailchimp_List_InvalidMergeField",  
  58. "List_InvalidInterestGroup" => "M2_Mailchimp_List_InvalidInterestGroup",  
  59. "List_TooManyInterestGroups" => "M2_Mailchimp_List_TooManyInterestGroups",  
  60. "Campaign_DoesNotExist" => "M2_Mailchimp_Campaign_DoesNotExist",  
  61. "Campaign_StatsNotAvailable" => "M2_Mailchimp_Campaign_StatsNotAvailable",  
  62. "Campaign_InvalidAbsplit" => "M2_Mailchimp_Campaign_InvalidAbsplit",  
  63. "Campaign_InvalidContent" => "M2_Mailchimp_Campaign_InvalidContent",  
  64. "Campaign_InvalidOption" => "M2_Mailchimp_Campaign_InvalidOption",  
  65. "Campaign_InvalidStatus" => "M2_Mailchimp_Campaign_InvalidStatus",  
  66. "Campaign_NotSaved" => "M2_Mailchimp_Campaign_NotSaved",  
  67. "Campaign_InvalidSegment" => "M2_Mailchimp_Campaign_InvalidSegment",  
  68. "Campaign_InvalidRss" => "M2_Mailchimp_Campaign_InvalidRss",  
  69. "Campaign_InvalidAuto" => "M2_Mailchimp_Campaign_InvalidAuto",  
  70. "MC_ContentImport_InvalidArchive" => "M2_Mailchimp_MC_ContentImport_InvalidArchive",  
  71. "Campaign_BounceMissing" => "M2_Mailchimp_Campaign_BounceMissing",  
  72. "Campaign_InvalidTemplate" => "M2_Mailchimp_Campaign_InvalidTemplate",  
  73. "Invalid_EcommOrder" => "M2_Mailchimp_Invalid_EcommOrder",  
  74. "Absplit_UnknownError" => "M2_Mailchimp_Absplit_UnknownError",  
  75. "Absplit_UnknownSplitTest" => "M2_Mailchimp_Absplit_UnknownSplitTest",  
  76. "Absplit_UnknownTestType" => "M2_Mailchimp_Absplit_UnknownTestType",  
  77. "Absplit_UnknownWaitUnit" => "M2_Mailchimp_Absplit_UnknownWaitUnit",  
  78. "Absplit_UnknownWinnerType" => "M2_Mailchimp_Absplit_UnknownWinnerType",  
  79. "Absplit_WinnerNotSelected" => "M2_Mailchimp_Absplit_WinnerNotSelected",  
  80. "Invalid_Analytics" => "M2_Mailchimp_Invalid_Analytics",  
  81. "Invalid_DateTime" => "M2_Mailchimp_Invalid_DateTime",  
  82. "Invalid_Email" => "M2_Mailchimp_Invalid_Email",  
  83. "Invalid_SendType" => "M2_Mailchimp_Invalid_SendType",  
  84. "Invalid_Template" => "M2_Mailchimp_Invalid_Template",  
  85. "Invalid_TrackingOptions" => "M2_Mailchimp_Invalid_TrackingOptions",  
  86. "Invalid_Options" => "M2_Mailchimp_Invalid_Options",  
  87. "Invalid_Folder" => "M2_Mailchimp_Invalid_Folder",  
  88. "Invalid_URL" => "M2_Mailchimp_Invalid_URL",  
  89. "Module_Unknown" => "M2_Mailchimp_Module_Unknown",  
  90. "MonthlyPlan_Unknown" => "M2_Mailchimp_MonthlyPlan_Unknown",  
  91. "Order_TypeUnknown" => "M2_Mailchimp_Order_TypeUnknown",  
  92. "Invalid_PagingLimit" => "M2_Mailchimp_Invalid_PagingLimit",  
  93. "Invalid_PagingStart" => "M2_Mailchimp_Invalid_PagingStart",  
  94. "Max_Size_Reached" => "M2_Mailchimp_Max_Size_Reached",  
  95. "MC_SearchException" => "M2_Mailchimp_MC_SearchException",  
  96. "Goal_SaveFailed" => "M2_Mailchimp_Goal_SaveFailed",  
  97. "Conversation_DoesNotExist" => "M2_Mailchimp_Conversation_DoesNotExist",  
  98. "Conversation_ReplySaveFailed" => "M2_Mailchimp_Conversation_ReplySaveFailed",  
  99. "File_Not_Found_Exception" => "M2_Mailchimp_File_Not_Found_Exception",  
  100. "Folder_Not_Found_Exception" => "M2_Mailchimp_Folder_Not_Found_Exception",  
  101. "Folder_Exists_Exception" => "M2_Mailchimp_Folder_Exists_Exception" 
  102. ); 
  103.  
  104. public function __construct($apikey=null, $opts=array()) { 
  105. if (!$apikey) { 
  106. $apikey = getenv('MAILCHIMP_APIKEY'); 
  107.  
  108. if (!$apikey) { 
  109. $apikey = $this->readConfigs(); 
  110.  
  111. if (!$apikey) { 
  112. throw new M2_Mailchimp_Error('You must provide a MailChimp API key'); 
  113.  
  114. $this->apikey = $apikey; 
  115. $dc = "us1"; 
  116.  
  117. if (strstr($this->apikey, "-")) { 
  118. list($key, $dc) = explode("-", $this->apikey, 2); 
  119. if (!$dc) { 
  120. $dc = "us1"; 
  121.  
  122. $this->root = str_replace('https://api', 'https://' . $dc . '.api', $this->root); 
  123. $this->root = rtrim($this->root, '/') . '/'; 
  124.  
  125. if (!isset($opts['timeout']) || !is_int($opts['timeout'])) { 
  126. $opts['timeout'] = 600; 
  127. if (isset($opts['debug'])) { 
  128. $this->debug = true; 
  129.  
  130.  
  131. $this->ch = curl_init(); 
  132.  
  133. if ( isset($opts['CURLOPT_FOLLOWLOCATION'] ) && $opts['CURLOPT_FOLLOWLOCATION'] === true) { 
  134. curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); 
  135.  
  136. curl_setopt($this->ch, CURLOPT_USERAGENT, 'MailChimp-PHP/2.0.5'); 
  137. curl_setopt($this->ch, CURLOPT_POST, true); 
  138. curl_setopt($this->ch, CURLOPT_HEADER, false); 
  139. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); 
  140. curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30); 
  141. curl_setopt($this->ch, CURLOPT_TIMEOUT, $opts['timeout']); 
  142.  
  143.  
  144. $this->folders = new M2_Mailchimp_Folders($this); 
  145. $this->templates = new M2_Mailchimp_Templates($this); 
  146. $this->users = new M2_Mailchimp_Users($this); 
  147. $this->helper = new M2_Mailchimp_Helper($this); 
  148. $this->mobile = new M2_Mailchimp_Mobile($this); 
  149. $this->conversations = new M2_Mailchimp_Conversations($this); 
  150. $this->ecomm = new M2_Mailchimp_Ecomm($this); 
  151. $this->neapolitan = new M2_Mailchimp_Neapolitan($this); 
  152. $this->lists = new M2_Mailchimp_Lists($this); 
  153. $this->campaigns = new M2_Mailchimp_Campaigns($this); 
  154. $this->vip = new M2_Mailchimp_Vip($this); 
  155. $this->reports = new M2_Mailchimp_Reports($this); 
  156. $this->gallery = new M2_Mailchimp_Gallery($this); 
  157. $this->goal = new M2_Mailchimp_Goal($this); 
  158.  
  159. public function __destruct() { 
  160. curl_close($this->ch); 
  161.  
  162. public function call($url, $params) { 
  163. $params['apikey'] = $this->apikey; 
  164.  
  165. $params = json_encode($params); 
  166. $ch = $this->ch; 
  167.  
  168. curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json'); 
  169. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
  170. curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
  171. curl_setopt($ch, CURLOPT_VERBOSE, $this->debug); 
  172.  
  173. $start = microtime(true); 
  174. $this->log('Call to ' . $this->root . $url . '.json: ' . $params); 
  175. if($this->debug) { 
  176. $curl_buffer = fopen('php://memory', 'w+'); 
  177. curl_setopt($ch, CURLOPT_STDERR, $curl_buffer); 
  178.  
  179. $response_body = curl_exec($ch); 
  180.  
  181. $info = curl_getinfo($ch); 
  182. $time = microtime(true) - $start; 
  183. if($this->debug) { 
  184. rewind($curl_buffer); 
  185. $this->log(stream_get_contents($curl_buffer)); 
  186. fclose($curl_buffer); 
  187. $this->log('Completed in ' . number_format($time * 1000, 2) . 'ms'); 
  188. $this->log('Got response: ' . $response_body); 
  189.  
  190. if(curl_error($ch)) { 
  191. throw new M2_Mailchimp_HttpError("API call to $url failed: " . curl_error($ch)); 
  192. $result = json_decode($response_body, true); 
  193.  
  194. if(floor($info['http_code'] / 100) >= 4) { 
  195. throw $this->castError($result); 
  196.  
  197. return $result; 
  198.  
  199. public function readConfigs() { 
  200. $paths = array('~/.mailchimp.key', '/etc/mailchimp.key'); 
  201. foreach($paths as $path) { 
  202. if(file_exists($path)) { 
  203. $apikey = trim(file_get_contents($path)); 
  204. if ($apikey) { 
  205. return $apikey; 
  206. return false; 
  207.  
  208. public function castError($result) { 
  209. if ($result['status'] !== 'error' || !$result['name']) { 
  210. throw new M2_Mailchimp_Error('We received an unexpected error: ' . json_encode($result)); 
  211.  
  212. $class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'M2_Mailchimp_Error'; 
  213. return new $class($result['error'], $result['code']); 
  214.  
  215. public function log($msg) { 
  216. if ($this->debug) { 
  217. error_log($msg);