├── LICENSE ├── README.md ├── composer.json └── src └── VPS └── MailChimp.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Drew McLellan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MailChimp API 3.0 Wrapper 2 | ============= 3 | 4 | Very Easy to use MailChimp REST Enabled API 3.0 Wrapper Class in PHP. 5 | 6 | Requires PHP 5.3 and curl extension. 7 | 8 | Installation 9 | ------------ 10 | 11 | You can install the mailchimp-rest-api using Composer. Just add the following to your composer.json: 12 | 13 | { 14 | "require": { 15 | "vatps/mailchimp-rest-api": "dev-master" 16 | } 17 | } 18 | 19 | You will then need to: 20 | * run ``composer install`` to get these dependencies added to your vendor directory 21 | * add the autoloader to your application with this line: ``require("vendor/autoload.php")`` 22 | 23 | Alternatively you can just download the MailChimp.php file and include it manually. 24 | 25 | Laravel Installation 26 | -------------------- 27 | 28 | Run ``composer require vatps/mailchimp-rest-api`` in terminal. 29 | 30 | Laravel Example 31 | --------------- 32 | 33 | setApiKey('yourapikeyhere-us1'); 38 | 39 | $result = $mc->get('/lists/'); 40 | return $result; 41 | 42 | You can find all available Resources at http://kb.mailchimp.com/api/resources 43 | 44 | Examples 45 | -------- 46 | 47 | Create New Instance and set mailchimp API Key. 48 | 49 | get('/lists/'); 56 | print_r($result); 57 | 58 | Lists Instance (GET /lists/{list_id}) 59 | 60 | get('/lists/{list_id}'); 62 | print_r($result); 63 | 64 | REMOVE LIST (DELETE /lists/{list_id}) 65 | 66 | delete('/lists/{list_id}'); 68 | 69 | Subscribe to a list (POST /lists/{list_id}/members) 70 | 71 | post('/lists/{list_id}/members', array( 73 | 'email_address' => 'test@vps.com', 74 | 'merge_fields' => array('FNAME'=>'VAT', 'LNAME'=>'PS'), 75 | 'status' => 'subscribed' 76 | )); 77 | print_r($result); 78 | 79 | Contact me if you need any help. 80 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vatps/mailchimp-rest-api", 3 | "description": "Very Easy to use MailChimp REST Enabled API 3.0 Wrapper Class.", 4 | "keywords": ["mailchimp", "php", "api", "class"], 5 | "homepage": "https://github.com/vatps/mailchimp-rest-api", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "VAT PS", 10 | "homepage": "http://vps.link" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.3.0", 15 | "ext-curl": "*" 16 | }, 17 | "autoload": { 18 | "psr-0": { "VPS": "src/" } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/VPS/MailChimp.php: -------------------------------------------------------------------------------- 1 | .api.mailchimp.com/3.0/'; 10 | 11 | /** 12 | * Create a new instance 13 | * @param string $apiKey - Optional 14 | */ 15 | public function __construct($apiKey = false) 16 | { 17 | if ($apiKey) { 18 | $this->apiKey = $apiKey; 19 | list(, $datacentre) = explode('-', $this->apiKey); 20 | $this->apiUrl = str_replace('', $datacentre, $this->apiUrl); 21 | } 22 | } 23 | 24 | /** 25 | * Method to Set Api Key 26 | * @param string $apiKey 27 | */ 28 | public function setApiKey($apiKey) 29 | { 30 | $this->apiKey = $apiKey; 31 | list(, $datacentre) = explode('-', $this->apiKey); 32 | $this->apiUrl = str_replace('', $datacentre, $this->apiUrl); 33 | } 34 | 35 | /** 36 | * Magic Method to request http verb 37 | * @return array 38 | */ 39 | public function __call($method, $arguments) 40 | { 41 | $httpVerb = strtoupper($method); 42 | $allowedHttpVerbs = array('GET', 'POST', 'PUT', 'PATCH', 'DELETE'); 43 | 44 | //Validate http verb 45 | if (in_array($httpVerb, $allowedHttpVerbs)) { 46 | $endPoint = $arguments[0]; 47 | $data = isset($arguments[1]) ? $arguments[1] : array(); 48 | return $this->request($endPoint, $httpVerb, $data); 49 | } 50 | 51 | throw new \Exception('Invalid http verb!'); 52 | } 53 | 54 | /** 55 | * Call MailChimp API 56 | * @param string $endPoint - (http://kb.mailchimp.com/api/resources) 57 | * @param string $httpVerb 58 | * @param array $data - Optional 59 | * @return array 60 | */ 61 | public function request($endPoint, $httpVerb = 'GET', $data = array()) 62 | { 63 | // Validate API 64 | if (!$this->apiKey) { 65 | throw new \Exception('MailChimp API Key must be set before making request!'); 66 | } 67 | 68 | $endPoint = ltrim($endPoint, '/'); 69 | $httpVerb = strtoupper($httpVerb); 70 | $requestUrl = $this->apiUrl.$endPoint; 71 | 72 | return $this->curlRequest($requestUrl, $httpVerb, $data); 73 | } 74 | 75 | /** 76 | * Request using curl extension 77 | * @param string $url 78 | * @param string $httpVerb 79 | * @param array $data - Optional 80 | * @return array 81 | */ 82 | private function curlRequest($url, $httpVerb, array $data = array(), $curlTimeout = 15) 83 | { 84 | if (function_exists('curl_init') && function_exists('curl_setopt')) { 85 | $ch = curl_init(); 86 | curl_setopt($ch, CURLOPT_URL, $url); 87 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 88 | curl_setopt($ch, CURLOPT_USERAGENT, 'VPS/MC-API:3.0'); 89 | curl_setopt($ch, CURLOPT_TIMEOUT, $curlTimeout); 90 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 91 | curl_setopt($ch, CURLOPT_USERPWD, "user:".$this->apiKey); 92 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpVerb); 93 | 94 | //Submit data 95 | if (!empty($data)) { 96 | $jsonData = json_encode($data); 97 | curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); 98 | } 99 | 100 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 101 | $result = curl_exec($ch); 102 | curl_close($ch); 103 | 104 | return $result ? json_decode($result, true) : false; 105 | } 106 | 107 | throw new \Exception('curl extension is missing!'); 108 | } 109 | } 110 | --------------------------------------------------------------------------------