Advertisement
Guest User

Untitled

a guest
Jan 31st, 2018
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drewm;
  4.  
  5. /**
  6. * Super-simple, minimum abstraction MailChimp API v2 wrapper
  7. *
  8. * Uses curl if available, falls back to file_get_contents and HTTP stream.
  9. * This probably has more comments than code.
  10. *
  11. * Contributors:
  12. * Michael Minor <me@pixelbacon.com>
  13. * Lorna Jane Mitchell, github.com/lornajane
  14. *
  15. * @author Drew McLellan <drew.mclellan@gmail.com>
  16. * @version 1.1.1
  17. */
  18. class MailChimp
  19. {
  20. private $api_key;
  21. private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0';
  22. private $verify_ssl = false;
  23.  
  24. /**
  25. * Create a new instance
  26. * @param string $api_key Your MailChimp API key
  27. */
  28. public function __construct($api_key)
  29. {
  30. $this->api_key = $api_key;
  31. list(, $datacentre) = explode('-', $this->api_key);
  32. $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
  33. }
  34.  
  35. /**
  36. * Validates MailChimp API Key
  37. */
  38. public function validateApiKey()
  39. {
  40. $request = $this->call('helper/ping');
  41. return !empty($request);
  42. }
  43.  
  44. /**
  45. * Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in.
  46. * @param string $method The API method to call, e.g. 'lists/list'
  47. * @param array $args An array of arguments to pass to the method. Will be json-encoded for you.
  48. * @return array Associative array of json decoded API response.
  49. */
  50. public function call($method, $args = array(), $timeout = 10)
  51. {
  52. return $this->makeRequest($method, $args, $timeout);
  53. }
  54.  
  55. /**
  56. * Performs the underlying HTTP request. Not very exciting
  57. * @param string $method The API method to be called
  58. * @param array $args Assoc array of parameters to be passed
  59. * @return array Assoc array of decoded result
  60. */
  61. private function makeRequest($method, $args = array(), $timeout = 10)
  62. {
  63. $args['apikey'] = $this->api_key;
  64.  
  65. $url = $this->api_endpoint.'/'.$method.'.json';
  66. $json_data = json_encode($args);
  67.  
  68. if (function_exists('curl_init') && function_exists('curl_setopt')) {
  69. $ch = curl_init();
  70. curl_setopt($ch, CURLOPT_URL, $url);
  71. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  72. curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  74. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  75. curl_setopt($ch, CURLOPT_POST, true);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
  77. curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  78. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  79. curl_setopt($ch, CURLOPT_ENCODING, '');
  80. $result = curl_exec($ch);
  81. curl_close($ch);
  82. } else {
  83. $result = file_get_contents($url, null, stream_context_create(array(
  84. 'http' => array(
  85. 'protocol_version' => 1.1,
  86. 'user_agent' => 'PHP-MCAPI/2.0',
  87. 'method' => 'POST',
  88. 'header' => "Content-type: application/json\r\n".
  89. "Connection: close\r\n" .
  90. "Content-length: " . strlen($json_data) . "\r\n",
  91. 'content' => $json_data,
  92. ),
  93. )));
  94. }
  95.  
  96. return $result ? json_decode($result, true) : false;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement