curl([ 'url' => 'https://us18.api.mailchimp.com/3.0/' . $endpoint, 'post_fields' => json_encode($data, JSON_FORCE_OBJECT), 'method' => $method, 'header' => [ 'Authorization: Basic ' . base64_encode(':' . $this->setting->get('mailchimp_api_key')), 'Content-Type: application/x-www-form-urlencoded' ] ]); $response = json_decode($curl_response, true); if ($response === null) { throw new Exception('Invalid JSON'); } return $response; } /** * Subscribe an email address to the mailing list. This will only mark you * as "pending" so you have to click a link in the email to actually * subscribe. * * @param string $email_address The email address to subscribe. * * @throws Exception If subscribing to the mailing list fails for some * reason. For example, if already subscribed. * * @return array The MailChimp response. */ public function subscribe($email_address) { $method = 'POST'; $endpoint = 'lists/' . $this->setting->get('mailchimp_list_id') . '/members/' ; $data = [ 'email_address' => $email_address, 'status' => 'pending' ]; $response = $this->mailchimp_api($method, $endpoint, $data); if(isset($response['id']) === false) { throw new Exception('Could not subscribe to mailing list.'); } return $response; } }