From fc8b304199da9f9618bf3fb096f348edd4fbc776 Mon Sep 17 00:00:00 2001 From: Rick Date: Thu, 18 Sep 2025 13:01:05 -0500 Subject: [PATCH 1/2] Implemented third `MailChimp::__construct()` parameter `$source_ip` (defaults to null for backward compatibility) which allows an integrator to set an override interface IP address to in the CURL requests. Strict typing was left out to keep code style similar. When used in the curl connection method, the `CURLOPT_INTERFACE` is only implemented when a non-empty string value is present in the $source_ip class property. --- src/MailChimp.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/MailChimp.php b/src/MailChimp.php index e810836..fe9f5b1 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -15,6 +15,8 @@ class MailChimp private $api_key; private $api_endpoint = 'https://.api.mailchimp.com/3.0'; + private $source_ip; + const TIMEOUT = 10; /* SSL Verification @@ -36,7 +38,7 @@ 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."); @@ -44,6 +46,8 @@ public function __construct($api_key, $api_endpoint = null) $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."); @@ -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)'); From 7ae0be35bf36825ff5510837292e710155b3f13b Mon Sep 17 00:00:00 2001 From: Rick Date: Thu, 18 Sep 2025 13:03:40 -0500 Subject: [PATCH 2/2] Changed the `Accept` and `Content-Type` headers used in the CURL request to the standard `application/json` value. While this is not currently causing an issue, MailChimp's API is plain REST with JSON and not JSON:API. All examples in their documentation use `application/json`. This is just future proofing given their use of Akami WAF Gateways where this has the potential to cause issues in the future. --- src/MailChimp.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MailChimp.php b/src/MailChimp.php index fe9f5b1..4bff3f9 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -222,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 );