1
0
mirror of https://github.com/beestat/app.git synced 2025-05-24 02:14:03 -04:00
beestat/api/mailchimp.php
Jon Ziebell 790aaf354e Fixed #265 - Change logging from push to pull
Deceptively simple commit message. This is a massive change that completely reworks the most fundamental part of the API. Not only does it remove the push logging, it also restructures logging to give better insight into what happens during an API call.
2020-03-15 08:12:28 -04:00

81 lines
2.1 KiB
PHP

<?php
/**
* High level functionality for interacting with the Mailchimp API.
*
* @author Jon Ziebell
*/
class mailchimp extends external_api {
protected static $log_mysql = 'all';
protected static $cache = false;
protected static $cache_for = null;
/**
* Send an API call off to MailChimp
*
* @param string $method HTTP Method.
* @param string $endpoint API Endpoint.
* @param array $data API request data.
*
* @throws Exception If MailChimp did not return valid JSON.
*
* @return array The MailChimp response.
*/
private function mailchimp_api($method, $endpoint, $data) {
$curl_response = $this->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;
}
}