Skip to content

Commit

Permalink
Adds more error reporting options
Browse files Browse the repository at this point in the history
  • Loading branch information
drewm committed Jan 15, 2016
1 parent 03960b9 commit 6d945b5
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions src/MailChimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
class MailChimp
{
private $api_key;
private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0';
private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0';

/* SSL Verification
Read before disabling:
http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
*/
public $verify_ssl = true;
public $verify_ssl = true;

private $last_error = false;
private $last_response = array();

/**
* Create a new instance
Expand All @@ -33,10 +36,34 @@ public function __construct($api_key)
$this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
}

/**
* Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL
* @param string $email The subscriber's email address
* @return string Hashed version of the input
*/
public function subscriberHash($email)
{
return md5(strtolower($email));
}

/**
* Get the last error returned by either the network transport, or by the API.
* If something didn't work, this should contain the string describing the problem.
* @return Message describing the error
*/
public function getLastError()
{
return $this->last_error;
}

/**
* Get an array containing the HTTP headers and the body of the API response.
* @return array Assoc array with keys 'headers' and 'body'
*/
public function getLastResponse()
{
return $this->last_response;
}

public function delete($method, $args=array(), $timeout=10)
{
Expand Down Expand Up @@ -65,7 +92,7 @@ public function put($method, $args=array(), $timeout=10)

/**
* Performs the underlying HTTP request. Not very exciting
* @param string $$http_verb The HTTP verb to use: get, post, put, patch, delete
* @param string $http_verb The HTTP verb to use: get, post, put, patch, delete
* @param string $method The API method to be called
* @param array $args Assoc array of parameters to be passed
* @return array Assoc array of decoded result
Expand All @@ -76,6 +103,9 @@ private function makeRequest($http_verb, $method, $args=array(), $timeout=10)

$json_data = json_encode($args);

$this->last_error = false;
$this->last_response = array('headers'=>null, 'body'=>null);

if (function_exists('curl_init') && function_exists('curl_setopt')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
Expand Down Expand Up @@ -116,12 +146,29 @@ private function makeRequest($http_verb, $method, $args=array(), $timeout=10)
}


$result = curl_exec($ch);
$this->last_response['body'] = curl_exec($ch);
$this->last_response['headers'] = curl_getinfo($ch);

if (!$this->last_response['body']) {
$this->last_error = curl_error($ch);
}

curl_close($ch);
} else {
throw new \Exception("cURL support is required, but can't be found.");
}

return $result ? json_decode($result, true) : false;
if ($this->last_response['body']) {

$d = json_decode($this->last_response['body'], true);

if (isset($d['status']) && $d['status']!='200' && isset($d['detail'])) {
$this->last_error = sprintf('%d: %s', $d['status'], $d['detail']);
}

return $d;
}

return false;
}
}

0 comments on commit 6d945b5

Please sign in to comment.