Skip to content

Instantly share code, notes, and snippets.

@IronRunes
Last active November 21, 2015 14:17
Show Gist options
  • Save IronRunes/3a0b47788fd2cc9edff6 to your computer and use it in GitHub Desktop.
Save IronRunes/3a0b47788fd2cc9edff6 to your computer and use it in GitHub Desktop.
<?php
/** Mailchimp API 3.0 cUrl wrapper for PHP
*
* Just quick class to manage to interact with the MailChimp API.
* The heavy lifting is done in the request method.
* Note that this has been built for my use and is by no means an
* exhaustive wrapper. Check the MailChimp API docs for a lot more
* info and leverage the API for what you need.
* */
class Mailchimp {
private $key = 'The API key';
private $datacenter = 'us1';
private $baseUrl;
private $listUrl;
private $deepUrl;
private $mailchimpList;
protected $curl;
public function __construct($list = 'your default list ID, if any'){
$this->mailchimpList = $list;
$this->baseUrl = 'https://' . $this->datacenter . '.api.mailchimp.com/3.0/lists/';
$this->listUrl = $this->baseUrl . $this->mailchimpList . '/';
$this->deepUrl = $this->listUrl . '/members/';
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($this->curl, CURLOPT_USERPWD, "whatever:" . $this->key);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($this->curl, CURLINFO_HEADER_OUT, true);
}
/**
* @param $method valid REST method, MC supports GET, POST, PATCH, PUT, DELETE, this class only the first 3
* @param $depth depth of the request (from 1 = baseUrl to 3 = deepUrl) - see @method __construct
* @param $endpoint typically md5 of the subcsriber email
* @param $data data to send across to MailChimp (typically to add/update a subscriber)
*/
public function request($method, $depth = 3, $endpoint = null, $data = null){
//get depth url
switch($depth){
case 1:
$url = $this->baseUrl;
break;
case 2:
$url = $this->listUrl;
break;
case 3:
$url = $this->deepUrl;
break;
}
// optimize method for switching ...
$method = strtoupper($method);
// get correct method option for cURL - see MC API docs
switch($method){
case 'PATCH':
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
$url .= $endpoint;
if($data){
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
}
break;
case 'POST':
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
if ($data){
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
}
break;
case 'GET':
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
$url .= $endpoint;
if ($data){
$url .= '?' . http_build_query($data);
}
break;
}
curl_setopt($this->curl, CURLOPT_URL, $url);
$request = curl_exec($this->curl);
if(!$request){
var_dump(curl_error($this->curl));
}
curl_close($this->curl);
return $request;
}
public function getInfo(){
debug_array(curl_getinfo($this->curl));
}
public function close(){
self::__destruct();
}
public function __destruct(){
if(isset($this->curl) && is_resource($this->curl)){
curl_close($this->curl);
}
else {
unset($this->curl);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment