From e14896a8cf42f5ddca931f1035ca5b09d2117f7c Mon Sep 17 00:00:00 2001 From: matteo-websolution Date: Mon, 19 Mar 2018 11:25:14 +0100 Subject: [PATCH 1/5] added getErrorsArray method --- src/MailChimp.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/MailChimp.php b/src/MailChimp.php index 87102de..dadc362 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -8,7 +8,8 @@ * This wrapper: https://github.com/drewm/mailchimp-api * * @author Drew McLellan - * @version 2.5 + * @author Matteo Lazzarin + * @version 2.6 */ class MailChimp { @@ -27,6 +28,7 @@ class MailChimp private $last_error = ''; private $last_response = array(); private $last_request = array(); + private $errors_array = array(); /** * Create a new instance @@ -46,7 +48,7 @@ public function __construct($api_key, $api_endpoint = null) if ($api_endpoint === null) { if (strpos($this->api_key, '-') === false) { - throw new \Exception("Invalid MailChimp API key supplied."); + throw new \Exception("Invalid MailChimp API key `{$api_key}` supplied."); } list(, $data_center) = explode('-', $this->api_key); $this->api_endpoint = str_replace('', $data_center, $this->api_endpoint); @@ -110,6 +112,17 @@ public function getLastError() { return $this->last_error ?: false; } + + /** + * Get errors array, in case of multiple errors got back from body response. + * Generically, this errors are thrown from wrong inserted MERGE_FIELDS value, giving a 400 status code + * + * @return array the errors array + */ + public function getErrorsArray() + { + return $this->errors_array; + } /** * Get an array containing the HTTP headers and the body of the API response. @@ -272,6 +285,11 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se $formattedResponse = $this->formatResponse($response); curl_close($ch); + + if ( array_key_exists('errors', $formattedResponse ) ) + { + $this->errors_array = $formattedResponse['errors']; + } $isSuccess = $this->determineSuccess($response, $formattedResponse, $timeout); From c00a49ec8119f4b8672baa0916268bca9c29257d Mon Sep 17 00:00:00 2001 From: matteo-websolution Date: Mon, 19 Mar 2018 11:25:44 +0100 Subject: [PATCH 2/5] described getErrorsArray method use on Readme.MD --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index ee57cc8..f97fb9b 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,13 @@ if ($MailChimp->success()) { print_r($result); } else { echo $MailChimp->getLastError(); + if(!empty($MailChimp->getErrorsArray())){ + echo "\nErrors array messages:"; + foreach($MailChimp->getErrorsArray() as $error){ + echo "\n".$error['field'].":".$error['message']; + } + } + } ``` From 090e5392ab9ebb571d40be1a820f5a3d78693513 Mon Sep 17 00:00:00 2001 From: matteo-websolution Date: Mon, 19 Mar 2018 11:56:51 +0100 Subject: [PATCH 3/5] moved errors_array check inside determineSuccess method --- src/MailChimp.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/MailChimp.php b/src/MailChimp.php index dadc362..c9e8610 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -285,11 +285,6 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se $formattedResponse = $this->formatResponse($response); curl_close($ch); - - if ( array_key_exists('errors', $formattedResponse ) ) - { - $this->errors_array = $formattedResponse['errors']; - } $isSuccess = $this->determineSuccess($response, $formattedResponse, $timeout); @@ -469,6 +464,10 @@ private function determineSuccess($response, $formattedResponse, $timeout) $this->request_successful = true; return true; } + + if (array_key_exists('errors', $formattedResponse)){ + $this->errors_array = $formattedResponse['errors']; + } if (isset($formattedResponse['detail'])) { $this->last_error = sprintf('%d: %s', $formattedResponse['status'], $formattedResponse['detail']); From 579a6924fd5a208a6de0ce60b7953d997a6efdc7 Mon Sep 17 00:00:00 2001 From: Matteo Date: Mon, 19 Mar 2018 12:05:32 +0100 Subject: [PATCH 4/5] Update MailChimp.php rollback to corrected version --- src/MailChimp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MailChimp.php b/src/MailChimp.php index c9e8610..0303d82 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -48,7 +48,7 @@ public function __construct($api_key, $api_endpoint = null) if ($api_endpoint === null) { if (strpos($this->api_key, '-') === false) { - throw new \Exception("Invalid MailChimp API key `{$api_key}` supplied."); + throw new \Exception("Invalid MailChimp API key supplied."); } list(, $data_center) = explode('-', $this->api_key); $this->api_endpoint = str_replace('', $data_center, $this->api_endpoint); From eeb366bec6d192a06e82a065deeeebd8894bc34f Mon Sep 17 00:00:00 2001 From: matteo-websolution Date: Wed, 18 Apr 2018 16:01:39 +0200 Subject: [PATCH 5/5] small warning thrown from formattedResponse not being array, now fixed --- src/MailChimp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MailChimp.php b/src/MailChimp.php index 0303d82..cc5a10f 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -465,7 +465,7 @@ private function determineSuccess($response, $formattedResponse, $timeout) return true; } - if (array_key_exists('errors', $formattedResponse)){ + if (isset($formattedResponse['errors'])){ $this->errors_array = $formattedResponse['errors']; }