Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/MailChimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class MailChimp
private $api_key;
private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0';

private $source_ip;

const TIMEOUT = 10;

/* SSL Verification
Expand All @@ -36,14 +38,16 @@ class MailChimp
*
* @throws \Exception
*/
public function __construct($api_key, $api_endpoint = null)
public function __construct($api_key, $api_endpoint = null, $source_ip = null)
{
if (!function_exists('curl_init') || !function_exists('curl_setopt')) {
throw new \Exception("cURL support is required, but can't be found.");
}

$this->api_key = $api_key;

$this->source_ip = $source_ip;

if ($api_endpoint === null) {
if (strpos($this->api_key, '-') === false) {
throw new \Exception("Invalid MailChimp API key supplied.");
Expand Down Expand Up @@ -218,8 +222,8 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se
$response = $this->prepareStateForRequest($http_verb, $method, $url, $timeout);

$httpHeader = array(
'Accept: application/vnd.api+json',
'Content-Type: application/vnd.api+json',
'Accept: application/json',
'Content-Type: application/json',
'Authorization: apikey ' . $this->api_key
);

Expand All @@ -232,6 +236,9 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se
}

$ch = curl_init();
if($this->source_ip && is_string($this->source_ip)) {
curl_setopt($ch, CURLOPT_INTERFACE, $this->source_ip);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)');
Expand Down