NM_Mailchimp

The Mailchimp Subscription Form NM Mailchimp class.

Defined (1)

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

/classes/mailchimp/Mailchimp.php  
  1. class NM_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" => "Mailchimp_ValidationError",  
  10. "ServerError_MethodUnknown" => "Mailchimp_ServerError_MethodUnknown",  
  11. "ServerError_InvalidParameters" => "Mailchimp_ServerError_InvalidParameters",  
  12. "Unknown_Exception" => "Mailchimp_Unknown_Exception",  
  13. "Request_TimedOut" => "Mailchimp_Request_TimedOut",  
  14. "Zend_Uri_Exception" => "Mailchimp_Zend_Uri_Exception",  
  15. "PDOException" => "Mailchimp_PDOException",  
  16. "Avesta_Db_Exception" => "Mailchimp_Avesta_Db_Exception",  
  17. "XML_RPC2_Exception" => "Mailchimp_XML_RPC2_Exception",  
  18. "XML_RPC2_FaultException" => "Mailchimp_XML_RPC2_FaultException",  
  19. "Too_Many_Connections" => "Mailchimp_Too_Many_Connections",  
  20. "Parse_Exception" => "Mailchimp_Parse_Exception",  
  21. "User_Unknown" => "Mailchimp_User_Unknown",  
  22. "User_Disabled" => "Mailchimp_User_Disabled",  
  23. "User_DoesNotExist" => "Mailchimp_User_DoesNotExist",  
  24. "User_NotApproved" => "Mailchimp_User_NotApproved",  
  25. "Invalid_ApiKey" => "Mailchimp_Invalid_ApiKey",  
  26. "User_UnderMaintenance" => "Mailchimp_User_UnderMaintenance",  
  27. "Invalid_AppKey" => "Mailchimp_Invalid_AppKey",  
  28. "Invalid_IP" => "Mailchimp_Invalid_IP",  
  29. "User_DoesExist" => "Mailchimp_User_DoesExist",  
  30. "User_InvalidRole" => "Mailchimp_User_InvalidRole",  
  31. "User_InvalidAction" => "Mailchimp_User_InvalidAction",  
  32. "User_MissingEmail" => "Mailchimp_User_MissingEmail",  
  33. "User_CannotSendCampaign" => "Mailchimp_User_CannotSendCampaign",  
  34. "User_MissingModuleOutbox" => "Mailchimp_User_MissingModuleOutbox",  
  35. "User_ModuleAlreadyPurchased" => "Mailchimp_User_ModuleAlreadyPurchased",  
  36. "User_ModuleNotPurchased" => "Mailchimp_User_ModuleNotPurchased",  
  37. "User_NotEnoughCredit" => "Mailchimp_User_NotEnoughCredit",  
  38. "MC_InvalidPayment" => "Mailchimp_MC_InvalidPayment",  
  39. "List_DoesNotExist" => "Mailchimp_List_DoesNotExist",  
  40. "List_InvalidInterestFieldType" => "Mailchimp_List_InvalidInterestFieldType",  
  41. "List_InvalidOption" => "Mailchimp_List_InvalidOption",  
  42. "List_InvalidUnsubMember" => "Mailchimp_List_InvalidUnsubMember",  
  43. "List_InvalidBounceMember" => "Mailchimp_List_InvalidBounceMember",  
  44. "List_AlreadySubscribed" => "Mailchimp_List_AlreadySubscribed",  
  45. "List_NotSubscribed" => "Mailchimp_List_NotSubscribed",  
  46. "List_InvalidImport" => "Mailchimp_List_InvalidImport",  
  47. "MC_PastedList_Duplicate" => "Mailchimp_MC_PastedList_Duplicate",  
  48. "MC_PastedList_InvalidImport" => "Mailchimp_MC_PastedList_InvalidImport",  
  49. "Email_AlreadySubscribed" => "Mailchimp_Email_AlreadySubscribed",  
  50. "Email_AlreadyUnsubscribed" => "Mailchimp_Email_AlreadyUnsubscribed",  
  51. "Email_NotExists" => "Mailchimp_Email_NotExists",  
  52. "Email_NotSubscribed" => "Mailchimp_Email_NotSubscribed",  
  53. "List_MergeFieldRequired" => "Mailchimp_List_MergeFieldRequired",  
  54. "List_CannotRemoveEmailMerge" => "Mailchimp_List_CannotRemoveEmailMerge",  
  55. "List_Merge_InvalidMergeID" => "Mailchimp_List_Merge_InvalidMergeID",  
  56. "List_TooManyMergeFields" => "Mailchimp_List_TooManyMergeFields",  
  57. "List_InvalidMergeField" => "Mailchimp_List_InvalidMergeField",  
  58. "List_InvalidInterestGroup" => "Mailchimp_List_InvalidInterestGroup",  
  59. "List_TooManyInterestGroups" => "Mailchimp_List_TooManyInterestGroups",  
  60. "Campaign_DoesNotExist" => "Mailchimp_Campaign_DoesNotExist",  
  61. "Campaign_StatsNotAvailable" => "Mailchimp_Campaign_StatsNotAvailable",  
  62. "Campaign_InvalidAbsplit" => "Mailchimp_Campaign_InvalidAbsplit",  
  63. "Campaign_InvalidContent" => "Mailchimp_Campaign_InvalidContent",  
  64. "Campaign_InvalidOption" => "Mailchimp_Campaign_InvalidOption",  
  65. "Campaign_InvalidStatus" => "Mailchimp_Campaign_InvalidStatus",  
  66. "Campaign_NotSaved" => "Mailchimp_Campaign_NotSaved",  
  67. "Campaign_InvalidSegment" => "Mailchimp_Campaign_InvalidSegment",  
  68. "Campaign_InvalidRss" => "Mailchimp_Campaign_InvalidRss",  
  69. "Campaign_InvalidAuto" => "Mailchimp_Campaign_InvalidAuto",  
  70. "MC_ContentImport_InvalidArchive" => "Mailchimp_MC_ContentImport_InvalidArchive",  
  71. "Campaign_BounceMissing" => "Mailchimp_Campaign_BounceMissing",  
  72. "Campaign_InvalidTemplate" => "Mailchimp_Campaign_InvalidTemplate",  
  73. "Invalid_EcommOrder" => "Mailchimp_Invalid_EcommOrder",  
  74. "Absplit_UnknownError" => "Mailchimp_Absplit_UnknownError",  
  75. "Absplit_UnknownSplitTest" => "Mailchimp_Absplit_UnknownSplitTest",  
  76. "Absplit_UnknownTestType" => "Mailchimp_Absplit_UnknownTestType",  
  77. "Absplit_UnknownWaitUnit" => "Mailchimp_Absplit_UnknownWaitUnit",  
  78. "Absplit_UnknownWinnerType" => "Mailchimp_Absplit_UnknownWinnerType",  
  79. "Absplit_WinnerNotSelected" => "Mailchimp_Absplit_WinnerNotSelected",  
  80. "Invalid_Analytics" => "Mailchimp_Invalid_Analytics",  
  81. "Invalid_DateTime" => "Mailchimp_Invalid_DateTime",  
  82. "Invalid_Email" => "Mailchimp_Invalid_Email",  
  83. "Invalid_SendType" => "Mailchimp_Invalid_SendType",  
  84. "Invalid_Template" => "Mailchimp_Invalid_Template",  
  85. "Invalid_TrackingOptions" => "Mailchimp_Invalid_TrackingOptions",  
  86. "Invalid_Options" => "Mailchimp_Invalid_Options",  
  87. "Invalid_Folder" => "Mailchimp_Invalid_Folder",  
  88. "Invalid_URL" => "Mailchimp_Invalid_URL",  
  89. "Module_Unknown" => "Mailchimp_Module_Unknown",  
  90. "MonthlyPlan_Unknown" => "Mailchimp_MonthlyPlan_Unknown",  
  91. "Order_TypeUnknown" => "Mailchimp_Order_TypeUnknown",  
  92. "Invalid_PagingLimit" => "Mailchimp_Invalid_PagingLimit",  
  93. "Invalid_PagingStart" => "Mailchimp_Invalid_PagingStart",  
  94. "Max_Size_Reached" => "Mailchimp_Max_Size_Reached",  
  95. "MC_SearchException" => "Mailchimp_MC_SearchException",  
  96. "Goal_SaveFailed" => "Mailchimp_Goal_SaveFailed",  
  97. "Conversation_DoesNotExist" => "Mailchimp_Conversation_DoesNotExist",  
  98. "Conversation_ReplySaveFailed" => "Mailchimp_Conversation_ReplySaveFailed",  
  99. "File_Not_Found_Exception" => "Mailchimp_File_Not_Found_Exception",  
  100. "Folder_Not_Found_Exception" => "Mailchimp_Folder_Not_Found_Exception",  
  101. "Folder_Exists_Exception" => "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 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.6'); 
  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 Mailchimp_Folders($this); 
  145. $this->templates = new Mailchimp_Templates($this); 
  146. $this->users = new Mailchimp_Users($this); 
  147. $this->helper = new Mailchimp_Helper($this); 
  148. $this->mobile = new Mailchimp_Mobile($this); 
  149. $this->conversations = new Mailchimp_Conversations($this); 
  150. $this->ecomm = new Mailchimp_Ecomm($this); 
  151. $this->neapolitan = new Mailchimp_Neapolitan($this); 
  152. $this->lists = new Mailchimp_Lists($this); 
  153. $this->campaigns = new Mailchimp_Campaigns($this); 
  154. $this->vip = new Mailchimp_Vip($this); 
  155. $this->reports = new Mailchimp_Reports($this); 
  156. $this->gallery = new Mailchimp_Gallery($this); 
  157. $this->goal = new Mailchimp_Goal($this); 
  158.  
  159. public function __destruct() { 
  160. if(is_resource($this->ch)) { 
  161. curl_close($this->ch); 
  162.  
  163. public function call($url, $params) { 
  164. $params['apikey'] = $this->apikey; 
  165.  
  166. $params = json_encode($params); 
  167. $ch = $this->ch; 
  168.  
  169. curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json'); 
  170. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
  171. curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
  172. curl_setopt($ch, CURLOPT_VERBOSE, $this->debug); 
  173.  
  174. $start = microtime(true); 
  175. $this->log('Call to ' . $this->root . $url . '.json: ' . $params); 
  176. if($this->debug) { 
  177. $curl_buffer = fopen('php://memory', 'w+'); 
  178. curl_setopt($ch, CURLOPT_STDERR, $curl_buffer); 
  179.  
  180. $response_body = curl_exec($ch); 
  181.  
  182. $info = curl_getinfo($ch); 
  183. $time = microtime(true) - $start; 
  184. if($this->debug) { 
  185. rewind($curl_buffer); 
  186. $this->log(stream_get_contents($curl_buffer)); 
  187. fclose($curl_buffer); 
  188. $this->log('Completed in ' . number_format($time * 1000, 2) . 'ms'); 
  189. $this->log('Got response: ' . $response_body); 
  190.  
  191. if(curl_error($ch)) { 
  192. throw new Mailchimp_HttpError("API call to $url failed: " . curl_error($ch)); 
  193. $result = json_decode($response_body, true); 
  194.  
  195. if(floor($info['http_code'] / 100) >= 4) { 
  196. throw $this->castError($result); 
  197.  
  198. return $result; 
  199.  
  200. public function readConfigs() { 
  201. $paths = array('~/.mailchimp.key', '/etc/mailchimp.key'); 
  202. foreach($paths as $path) { 
  203. if(file_exists($path)) { 
  204. $apikey = trim(file_get_contents($path)); 
  205. if ($apikey) { 
  206. return $apikey; 
  207. return false; 
  208.  
  209. public function castError($result) { 
  210. if ($result['status'] !== 'error' || !$result['name']) { 
  211. throw new Mailchimp_Error('We received an unexpected error: ' . json_encode($result)); 
  212.  
  213. $class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mailchimp_Error'; 
  214. return new $class($result['error'], $result['code']); 
  215.  
  216. public function log($msg) { 
  217. if ($this->debug) { 
  218. error_log($msg);