3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Super-simple, minimum abstraction MailChimp API v3 wrapper * * Uses curl if available, falls back to file_get_contents and HTTP stream. * This probably has more comments than code. * * @author Drew McLellan <drew.mclellan@gmail.com> * @version 2.0 */ class MailChimp { private $api_key; private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0'; private $verify_ssl = true; /** * Create a new instance * @param string $api_key Your MailChimp API key */ public function __construct($api_key) { $this->api_key = $api_key; list(, $datacentre) = explode('-', $this->api_key); $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint); } public function delete($method, $args=array(), $timeout=10) { return $this->makeRequest('delete', $method, $args, $timeout); } public function get($method, $args=array(), $timeout=10) { return $this->makeRequest('get', $method, $args, $timeout); } public function patch($method, $args=array(), $timeout=10) { return $this->makeRequest('patch', $method, $args, $timeout); } public function post($method, $args=array(), $timeout=10) { return $this->makeRequest('post', $method, $args, $timeout); } public function put($method, $args=array(), $timeout=10) { return $this->makeRequest('put', $method, $args, $timeout); } /** * Performs the underlying HTTP request. Not very exciting * @param string $$http_verb The HTTP verb to use: get, post, put, patch, delete * @param string $method The API method to be called * @param array $args Assoc array of parameters to be passed * @return array Assoc array of decoded result */ private function makeRequest($http_verb, $method, $args=array(), $timeout=10) { $args['apikey'] = $this->api_key; $url = $this->api_endpoint.'/'.$method; $json_data = json_encode((object)$args); if (function_exists('curl_init') && function_exists('curl_setopt')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, 'drewm:'.$this->api_key); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.api+json', 'Content-Type: application/vnd.api+json')); curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); switch($http_verb) { case 'post': curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); break; case 'get': $query = http_build_query($args); curl_setopt($ch, CURLOPT_URL, $url.'?'.$query); break; case 'delete': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; case 'patch': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); break; } $result = curl_exec($ch); curl_close($ch); } else { throw new Exception("cURL support is required, but can't be found."); } return $result ? json_decode($result, true) : false; } } // MailChimp Configuration $api_key = "f36426878616bc7a2e1fc918ab9f9de5-us12"; // ENTER YOUR API KEY HERE $list_id = "d7973804f4"; // ENTER YOUR LIST ID HERE $MailChimp = new MailChimp($api_key); $result = $MailChimp->post('lists/' .$list_id. '/members', array( 'email_address' => 'something@gmail.com', 'status' => 'subscribed' )); if($result['status'] == 'subscribed') { echo json_encode(array('error' => false, 'message' => 'Thanks for subscribing with us')); exit; } else { echo json_encode(array('error' => true, 'message' => $result['title'])); exit; }
based on pXdJ5
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught Exception: cURL support is required, but can't be found. in /in/A2uEv:109 Stack trace: #0 /in/A2uEv(47): MailChimp->makeRequest('post', 'lists/d7973804f...', Array, 10) #1 /in/A2uEv(122): MailChimp->post('lists/d7973804f...', Array) #2 {main} thrown in /in/A2uEv on line 109
Process exited with code 255.
Output for 5.2.0 - 5.6.28
Fatal error: Uncaught exception 'Exception' with message 'cURL support is required, but can't be found.' in /in/A2uEv:109 Stack trace: #0 /in/A2uEv(47): MailChimp->makeRequest('post', 'lists/d7973804f...', Array, 10) #1 /in/A2uEv(124): MailChimp->post('lists/d7973804f...', Array) #2 {main} thrown in /in/A2uEv on line 109
Process exited with code 255.
Output for 5.0.0 - 5.1.6
Fatal error: Call to undefined function json_encode() in /in/A2uEv on line 68
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/A2uEv on line 15
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/A2uEv on line 15
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/A2uEv on line 15
Process exited with code 255.