DrewmMailChimp

Super-simple, minimum abstraction MailChimp API v3 wrapper MailChimp API v3: http://developer.mailchimp.com This wrapper: https://github.com/drewm/mailchimp-api.

Defined (1)

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

/includes/MailChimp.php  
  1. class MailChimp 
  2. private $api_key; 
  3. private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0'; 
  4. private $verify_ssl = false; 
  5.  
  6. /** 
  7. * Create a new instance 
  8. * @param string $api_key Your MailChimp API key 
  9. */ 
  10. function __construct($api_key) 
  11. $this->api_key = $api_key; 
  12. list(, $datacentre) = explode('-', $this->api_key); 
  13. $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint); 
  14.  
  15. /** 
  16. * Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in. 
  17. * @param string $method The API method to call, e.g. 'lists/list' 
  18. * @param array $args An array of arguments to pass to the method. Will be json-encoded for you. 
  19. * @return array Associative array of json decoded API response. 
  20. */ 
  21. public function call($method, $args=array()) 
  22. return $this->makeRequest($method, $args); 
  23.  
  24. /** 
  25. * Performs the underlying HTTP request. Not very exciting 
  26. * @param string $method The API method to be called 
  27. * @param array $args Assoc array of parameters to be passed 
  28. * @return array Assoc array of decoded result 
  29. */ 
  30. private function makeRequest($method, $args=array()) 
  31. {  
  32. $args['apikey'] = $this->api_key; 
  33.  
  34. $url = $this->api_endpoint.'/'.$method.'.json'; 
  35.  
  36. if (function_exists('curl_init') && function_exists('curl_setopt')) { 
  37. $ch = curl_init(); 
  38. curl_setopt($ch, CURLOPT_URL, $url); 
  39. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
  40. curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');  
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  42. curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
  43. curl_setopt($ch, CURLOPT_POST, true); 
  44. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl); 
  45. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args)); 
  46. $result = curl_exec($ch); 
  47. curl_close($ch); 
  48. } else { 
  49. $json_data = json_encode($args); 
  50. $result = file_get_contents($url, null, stream_context_create(array( 
  51. 'http' => array( 
  52. 'protocol_version' => 1.1,  
  53. 'user_agent' => 'PHP-MCAPI/2.0',  
  54. 'method' => 'POST',  
  55. 'header' => "Content-type: application/json\r\n". 
  56. "Connection: close\r\n" . 
  57. "Content-length: " . strlen($json_data) . "\r\n",  
  58. 'content' => $json_data,  
  59. ),  
  60. ))); 
  61.  
  62. return $result ? json_decode($result, true) : false;