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']; + } + } + } ``` diff --git a/src/MailChimp.php b/src/MailChimp.php index 87102de..cc5a10f 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 @@ -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. @@ -451,6 +464,10 @@ private function determineSuccess($response, $formattedResponse, $timeout) $this->request_successful = true; return true; } + + if (isset($formattedResponse['errors'])){ + $this->errors_array = $formattedResponse['errors']; + } if (isset($formattedResponse['detail'])) { $this->last_error = sprintf('%d: %s', $formattedResponse['status'], $formattedResponse['detail']);