getLastResponse() public method

Get an array containing the HTTP headers and the body of the API response.
public getLastResponse ( ) : array
return array Assoc array with keys 'headers' and 'body'
 /**
  * Handles responses from the MailChimp API.
  *
  * @param MailChimp $mailChimp
  * @return Array
  */
 public function handleMailChimpResponse($mailChimp)
 {
     $response = $mailChimp->getLastResponse();
     if (!$mailChimp->success()) {
         $message = $response && array_key_exists($response['errors']) ? $response['errors'][0]['message'] : 'Error connecting to MailChimp API';
         user_error($message, E_USER_ERROR);
     }
     return Convert::json2array($response['body']);
 }
Beispiel #2
0
 /**
  * Make a request to the Mailchimp API.
  *
  * @param  string $method   Request method (get, post, patch etc)
  * @param  string $endpoint Endpoint URL (e.g 'lists')
  * @param  array  $args     Request arguments
  * @return array            Response data
  *
  * @throws \Exception On error
  */
 private function request($method, $endpoint, $args = [])
 {
     $data = $this->mc->{$method}($endpoint, $args, 5);
     $response = $this->mc->getLastResponse();
     if (isset($response['headers']['http_code'])) {
         switch (substr($response['headers']['http_code'], 0, 1)) {
             case '4':
                 throw new Exception($data['title'] . ': ' . $data['detail']);
             case '5':
                 throw new Exception('Mailchimp API 500 error. Service may be unavailable.');
             default:
                 return $data;
         }
     } else {
         throw new Exception('Unknown Mailchimp error: ' . $this->mc->getLastError());
     }
 }