1414class MailChimp
1515{
1616 private $ api_key ;
17- private $ api_endpoint = 'https://<dc>.api.mailchimp.com/3.0 ' ;
17+ private $ api_endpoint = 'https://<dc>.api.mailchimp.com/3.0 ' ;
1818
1919 /* SSL Verification
2020 Read before disabling:
2121 http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
2222 */
23- public $ verify_ssl = true ;
23+ public $ verify_ssl = true ;
24+
25+ private $ last_error = false ;
26+ private $ last_response = array ();
2427
2528 /**
2629 * Create a new instance
@@ -33,10 +36,34 @@ public function __construct($api_key)
3336 $ this ->api_endpoint = str_replace ('<dc> ' , $ datacentre , $ this ->api_endpoint );
3437 }
3538
39+ /**
40+ * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL
41+ * @param string $email The subscriber's email address
42+ * @return string Hashed version of the input
43+ */
3644 public function subscriberHash ($ email )
3745 {
3846 return md5 (strtolower ($ email ));
3947 }
48+
49+ /**
50+ * Get the last error returned by either the network transport, or by the API.
51+ * If something didn't work, this should contain the string describing the problem.
52+ * @return Message describing the error
53+ */
54+ public function getLastError ()
55+ {
56+ return $ this ->last_error ;
57+ }
58+
59+ /**
60+ * Get an array containing the HTTP headers and the body of the API response.
61+ * @return array Assoc array with keys 'headers' and 'body'
62+ */
63+ public function getLastResponse ()
64+ {
65+ return $ this ->last_response ;
66+ }
4067
4168 public function delete ($ method , $ args =array (), $ timeout =10 )
4269 {
@@ -65,7 +92,7 @@ public function put($method, $args=array(), $timeout=10)
6592
6693 /**
6794 * Performs the underlying HTTP request. Not very exciting
68- * @param string $$ http_verb The HTTP verb to use: get, post, put, patch, delete
95+ * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete
6996 * @param string $method The API method to be called
7097 * @param array $args Assoc array of parameters to be passed
7198 * @return array Assoc array of decoded result
@@ -76,6 +103,9 @@ private function makeRequest($http_verb, $method, $args=array(), $timeout=10)
76103
77104 $ json_data = json_encode ($ args );
78105
106+ $ this ->last_error = false ;
107+ $ this ->last_response = array ('headers ' =>null , 'body ' =>null );
108+
79109 if (function_exists ('curl_init ' ) && function_exists ('curl_setopt ' )) {
80110 $ ch = curl_init ();
81111 curl_setopt ($ ch , CURLOPT_URL , $ url );
@@ -116,12 +146,29 @@ private function makeRequest($http_verb, $method, $args=array(), $timeout=10)
116146 }
117147
118148
119- $ result = curl_exec ($ ch );
149+ $ this ->last_response ['body ' ] = curl_exec ($ ch );
150+ $ this ->last_response ['headers ' ] = curl_getinfo ($ ch );
151+
152+ if (!$ this ->last_response ['body ' ]) {
153+ $ this ->last_error = curl_error ($ ch );
154+ }
155+
120156 curl_close ($ ch );
121157 } else {
122158 throw new \Exception ("cURL support is required, but can't be found. " );
123159 }
124160
125- return $ result ? json_decode ($ result , true ) : false ;
161+ if ($ this ->last_response ['body ' ]) {
162+
163+ $ d = json_decode ($ this ->last_response ['body ' ], true );
164+
165+ if (isset ($ d ['status ' ]) && $ d ['status ' ]!='200 ' && isset ($ d ['detail ' ])) {
166+ $ this ->last_error = sprintf ('%d: %s ' , $ d ['status ' ], $ d ['detail ' ]);
167+ }
168+
169+ return $ d ;
170+ }
171+
172+ return false ;
126173 }
127174}
0 commit comments