├── HypeMailchimpBundle.php
├── Mailchimp
├── Methods
│ ├── MCEcommerce.php
│ ├── MCHelper.php
│ ├── MCExport.php
│ ├── MCTemplate.php
│ ├── MCCampaign.php
│ └── MCList.php
├── MailchimpAPIException.php
├── RestClient.php
└── MailChimp.php
├── Resources
└── config
│ └── services.yml
├── DependencyInjection
├── HypeMailchimpExtension.php
└── Configuration.php
├── composer.json
├── LICENSE.md
└── README.md
/HypeMailchimpBundle.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 |
12 | namespace Hype\MailchimpBundle\Mailchimp;
13 |
14 | class MailchimpAPIException extends \Exception {
15 |
16 | public function __construct($data) {
17 | parent::__construct(sprintf('Mailchimp API error : [ %s ] %s , code = %s', $data['name'], $data['error'], $data['code']),$data['code']);
18 | }
19 |
20 | }
--------------------------------------------------------------------------------
/DependencyInjection/HypeMailchimpExtension.php:
--------------------------------------------------------------------------------
1 | processConfiguration($configuration, $configs);
23 |
24 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
25 | $loader->load('services.yml');
26 | $container->setParameter('hypemailchimp.config',$config);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ahmedsamy/hype-mailchimp-bundle",
3 | "license": "MIT",
4 | "type": "symfony-bundle",
5 | "description": "Mailchimp V2.0 API Object Oriented wrapper",
6 | "homepage": "https://github.com/AhmedSamy/hype-mailchimp-api-2.0",
7 | "keywords": ["mailchimp","api","newsletter","http client", "curl"],
8 | "authors": [
9 | {
10 | "name": "Ahmed Samy",
11 | "email": "ahmed.samy.cs@gmail.com",
12 | "homepage":"http://www.ahmed-samy.com",
13 | "role":"Developer"
14 | }
15 | ],
16 | "autoload": {
17 | "psr-0": { "Hype\\MailchimpBundle": "" }
18 | },
19 | "target-dir": "Hype/MailchimpBundle",
20 | "require": {
21 | "php": ">=5.3.2",
22 | "kriswallsmith/buzz":">=0.7 <0.17",
23 | "symfony/framework-bundle": "~2.0|~3.0|~4.0",
24 | "kriswallsmith/buzz":">=0.7",
25 | "ext-curl": "*"
26 | },
27 | "minimum-stability": "dev",
28 | "extra": {
29 | "branch-alias": {
30 | "dev-master": "1.1-dev"
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Ahmed Samy
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all 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,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/DependencyInjection/Configuration.php:
--------------------------------------------------------------------------------
1 | getRootNode() : $treeBuilder->root('hype_mailchimp');
15 |
16 | // Here you should define the parameters that are allowed to
17 | // configure your bundle. See the documentation linked above for
18 | // more information on that topic.
19 |
20 | $rootNode->children()
21 | ->scalarNode('api_key')->isRequired()->cannotBeEmpty()->end()
22 | ->scalarNode('default_list')->isRequired()->cannotBeEmpty()->end()
23 | ->booleanNode('ssl')->defaultTrue()->end()
24 | ->integerNode('timeout')->defaultValue(20)->end()
25 | ->end();
26 |
27 | return $treeBuilder;
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Mailchimp/Methods/MCHelper.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 |
12 | namespace Hype\MailchimpBundle\Mailchimp\Methods;
13 |
14 | use Hype\MailchimpBundle\Mailchimp\RestClient,
15 | Hype\MailchimpBundle\Mailchimp\MailchimpAPIException;
16 |
17 | class MCHelper extends RestClient
18 | {
19 |
20 | /**
21 | * Ping the MailChimp API
22 | *
23 | * @return string
24 | * @throws MailchimpAPIException
25 | */
26 | public function ping()
27 | {
28 | $apiCall = '/helper/ping';
29 | $payload = "";
30 | $data = $this->requestMonkey($apiCall, $payload);
31 | $data = json_decode($data, true);
32 | if (isset($data['error']))
33 | throw new MailchimpAPIException($data);
34 | else
35 | return $data['msg'];
36 | }
37 |
38 | /**
39 | * Have HTML content auto-converted to a text-only format. You can send: plain HTML, an existing Campaign Id, or an existing Template Id
40 | *
41 | * @param string $type
42 | * @param array $content
43 | * @return array
44 | * @throws MailchimpAPIException
45 | */
46 | public function generateText($type = 'html', $content = array())
47 | {
48 | $apiCall = '/helper/generate-text';
49 | $payload = array(
50 | 'type' => $type,
51 | 'content' => $content
52 | );
53 |
54 | $data = $this->requestMonkey($apiCall, $payload);
55 | $data = json_decode($data, true);
56 | if (isset($data['error']))
57 | throw new MailchimpAPIException($data);
58 | else
59 | return $data;
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/Mailchimp/RestClient.php:
--------------------------------------------------------------------------------
1 | config = $config;
24 | $this->listId = $listId;
25 | $this->dataCenter = $dataCenter;
26 | }
27 |
28 | /**
29 | * Prepare the curl request
30 | *
31 | * @param string $apiCall the API call function
32 | * @param array $payload Parameters
33 | * @param boolean $export indicate wether API used is Export API or not
34 | * @return array
35 | */
36 | protected function requestMonkey($apiCall, $payload, $export = false)
37 | {
38 | $payload['apikey'] = $this->config['api_key'];
39 |
40 | if ($export) {
41 | $url = $this->dataCenter . $apiCall;
42 | } else {
43 | $url = $this->dataCenter . '2.0/' . $apiCall;
44 | }
45 | $curl = $this->prepareCurl();
46 | $browser = new Browser($curl);
47 | $payload = json_encode($payload);
48 | $headers = array(
49 | "Accept" => "application/json",
50 | "Content-type" => "application/json"
51 | );
52 | $response = $browser->post($url, $headers, $payload);
53 |
54 | return $response->getContent();
55 | }
56 |
57 | /**
58 | * resolve curl configuration
59 | * @return Curl
60 | */
61 | private function prepareCurl()
62 | {
63 | $curl = new Curl();
64 | $curl->setOption(CURLOPT_USERAGENT, 'HypeMailchimp');
65 | $curl->setVerifyPeer(false);
66 | $curl->setTimeout($this->config['timeout']);
67 | return $curl;
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/Mailchimp/Methods/MCExport.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 |
12 | namespace Hype\MailchimpBundle\Mailchimp\Methods;
13 |
14 | use Hype\MailchimpBundle\Mailchimp\RestClient;
15 |
16 | class MCExport extends RestClient {
17 |
18 | protected $url = 'export/1.0/';
19 |
20 | /**
21 | * Dump members of a list
22 | * @link http://apidocs.mailchimp.com/export/1.0/list.func.php Read mailchimp api docs
23 | * @param array $options
24 | * @param string $listId
25 | * @return array
26 | */
27 | public function DumpList($options = array(), $listId = false) {
28 | $api = $this->url . 'list/';
29 | if (!$listId)
30 | $listId = $this->listId;
31 | $payload = array_merge(array('id' => $this->listId), $options);
32 | $data = $this->requestMonkey($api, $payload, true);
33 | if (empty($data) || !isset($data))
34 | return $data;
35 | $result = preg_split('/$\R?^/m', $data);
36 |
37 | $headerArray = json_decode($result[0]);
38 | unset($result[0]);
39 |
40 | $data = array();
41 | foreach ($result as $value) {
42 | $data[] = array_combine($headerArray, json_decode($value));
43 | }
44 |
45 | return $data;
46 | }
47 |
48 | /**
49 | * Exports/dumps all Subscriber Activity for the requested campaign
50 | *
51 | * @link http://apidocs.mailchimp.com/export/1.0/campaignsubscriberactivity.func.php campaignSubscriberActivity method
52 | * @param int $id
53 | * @param array $options
54 | * @return array
55 | */
56 | public function campaignSubscriberActivity($id, $options = array()) {
57 | $api = $api = $this->url . 'campaignSubscriberActivity/';
58 |
59 | $payload = array_merge(array('id' => $id), $options);
60 | $data = $this->requestMonkey($api, $payload, true);
61 |
62 | // If json_decode doesn't seem to work when there are separated objects
63 | if ($jData = json_decode($data,true)) {
64 | return $jData;
65 | }
66 | // We combine them into one object
67 | $data = preg_replace('/(}\s{)/',',',$data);
68 | return json_decode($data,true);
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/Mailchimp/MailChimp.php:
--------------------------------------------------------------------------------
1 | config = $config;
17 | $this->listId = $this->config['default_list'];
18 | $key = preg_split("/-/", $this->config['api_key']);
19 |
20 | if ($this->config['ssl']) {
21 | $this->dataCenter = 'https://' . $key[1] . '.api.mailchimp.com/';
22 | } else {
23 | $this->dataCenter = 'http://' . $key[1] . '.api.mailchimp.com/';
24 | }
25 |
26 | if (!function_exists('curl_init')) {
27 | throw new \Exception('This bundle needs the CURL PHP extension.');
28 | }
29 |
30 | }
31 |
32 | /**
33 | *
34 | * @param string $apiKey mailchimp API key
35 | */
36 | public function setApiKey($apiKey)
37 | {
38 | $this->config['api_key'] = $apiKey;
39 | $key = preg_split("/-/", $apiKey);
40 |
41 | if ($this->config['ssl']) {
42 | $this->dataCenter = 'https://' . $key[1] . '.api.mailchimp.com/';
43 | } else {
44 | $this->dataCenter = 'http://' . $key[1] . '.api.mailchimp.com/';
45 | }
46 | }
47 |
48 | /**
49 | * Verifies if given API key is equal to stored one.
50 | *
51 | * @param string $apiKey mailchimp API key
52 | *
53 | * @return bool Returns true if keys equal, false otherwise.
54 | */
55 | public function verifyApiKey($apiKey)
56 | {
57 | return $this->config['api_key'] == $apiKey;
58 | }
59 |
60 | /**
61 | * Set mailing list id
62 | *
63 | * @param string $listId mailing list id
64 | */
65 | public function setListID($listId)
66 | {
67 | $this->listId = $listId;
68 | }
69 |
70 |
71 | /**
72 | *
73 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCList
74 | */
75 | public function getList()
76 | {
77 | return new Methods\MCList($this->config, $this->listId, $this->dataCenter);
78 | }
79 |
80 | /**
81 | *
82 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCCampaign
83 | */
84 | public function getCampaign()
85 | {
86 | return new Methods\MCCampaign($this->config, $this->listId, $this->dataCenter);
87 | }
88 |
89 | /**
90 | *
91 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCExport
92 | */
93 | public function getExport()
94 | {
95 | return new Methods\MCExport($this->config, $this->listId, $this->dataCenter);
96 | }
97 |
98 | /**
99 | *
100 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCTemplate
101 | */
102 | public function getTemplate()
103 | {
104 | return new Methods\MCTemplate($this->config, $this->listId, $this->dataCenter);
105 | }
106 |
107 | /**
108 | *
109 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCHelper
110 | */
111 | public function getHelper()
112 | {
113 | return new Methods\MCHelper($this->config, $this->listId, $this->dataCenter);
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Hype MailchimpBundle for API V2.0
2 | ========================
3 |
4 | [![Latest Version on Packagist][ico-version]][link-packagist]
5 |
6 | [ico-version]: https://img.shields.io/packagist/v/ahmedsamy/hype-mailchimp-bundle.svg?style=flat-square
7 |
8 | [link-packagist]: https://packagist.org/packages/ahmedsamy/hype-mailchimp-bundle
9 |
10 |
11 | Symfony2.x bundle for
12 | [MailChimp](http://apidocs.mailchimp.com/api/2.0/) API V2 and [Export API](http://apidocs.mailchimp.com/export/1.0/) API V1
13 | Wrapper bundle that makes accessing Mailchimp functions easily in object oriented using method chaining
14 |
15 |
16 | **License**
17 |
18 | HypeMailChimp bundle released under MIT LICENSE
19 |
20 | #Supported API Methods
21 |
22 | **Campaigns related**
23 |
24 | 1. `campaigns/create`
25 | 2. `campaigns/content`
26 | 2. `campaigns/list`
27 | 2. `campaigns/delete`
28 | 2. `campaigns/pause`
29 | 2. `campaigns/ready`
30 | 2. `campaigns/replicate`
31 | 2. `campaigns/ready`
32 | 2. `campaigns/resume`
33 | 2. `campaigns/send`
34 | 2. `campaigns/send-test`
35 | 2. `campaigns/segment-test`
36 | 2. `campaigns/schedule`
37 | 2. `campaigns/schedule-batch`
38 | 2. `campaigns/unschedule`
39 | 2. `campaigns/update`
40 |
41 | **Lists related**
42 |
43 | 1. `lists/list`
44 | 1. `lists/abuse-reports`
45 | 1. `lists/activity`
46 | 1. `lists/subscribe`
47 | 1. `lists/unsubscribe`
48 | 1. `lists/member-info`
49 | 1. `lists/interest-groupings`
50 | 1. `lists/interest-grouping-add`
51 | 1. `lists/interest-grouping-del`
52 | 1. `lists/interest-grouping-update`
53 | 1. `lists/interest-group-add`
54 | 1. `lists/interest-group-update`
55 | 1. `lists/interest-group-del`
56 | 1. `lists/segments`
57 | 1. `lists/segment-test`
58 |
59 | **Templates related**
60 |
61 | 1. `templates/add`
62 | 1. `templates/list`
63 | 1. `templates/del`
64 | 1. `templates/info`
65 | 1. `templates/undel`
66 |
67 |
68 |
69 | **Export API**
70 |
71 | 1. `list`
72 | 2. `campaignSubscriberActivity`
73 |
74 | **Helper related**
75 |
76 | 1. `helper/ping`
77 | 1. `helper/generate-text`
78 |
79 | Need support for a method not on the list submit an [issue](https://github.com/AhmedSamy/HypeMailchimpBundle/issues/new)
80 |
81 | ## Setup
82 |
83 | ### Step 1: Download HypeMailchimp using composer
84 |
85 | Add HypeMailchimp in your composer.json:
86 |
87 | ```js
88 | {
89 | "require": {
90 | "ahmedsamy/hype-mailchimp-bundle": "dev-master"
91 | }
92 | }
93 | ```
94 |
95 | Now tell composer to download the bundle by running the command:
96 |
97 | ``` bash
98 | $ php composer.phar update "ahmedsamy/hype-mailchimp-bundle"
99 | ```
100 |
101 | Composer will install the bundle to your project's `vendor/ahmedsamy/hype` directory.
102 |
103 | ### Step 2: Enable the bundle
104 |
105 | Enable the bundle in the kernel:
106 |
107 | ``` php
108 | get('hype_mailchimp');
137 | ?>
138 | ```
139 |
140 | ##Examples
141 |
142 | ###Create new campaign
143 | ``` php
144 | get('hype_mailchimp');
146 | $data = $mc->getCampaign()->create('regular', array(
147 | 'list_id' => '93419bbdc0',
148 | 'subject' => 'test created subject',
149 | 'from_email' => 'ahmed.samy.cs@gmail.com',
150 | 'from_name' => 'Ahmed Samy',
151 | 'to_name' => 'fans'
152 | ), array(
153 | 'html' => '
Html content
',
154 | 'sections' => array(),
155 | 'text' => 'test',
156 | 'url' => 'http://www.example.com',
157 | 'archive' => 'test'
158 | ));
159 | var_dump($data);
160 | ?>
161 | ```
162 | ###Delete existing campaign
163 | ``` php
164 | get('hype_mailchimp');
166 | $data = $mc->getCampaign()
167 | ->setCi('1088b4ed65')
168 | ->del();
169 |
170 | var_dump($data);
171 | ?>
172 | ```
173 |
174 | ###Send campaign
175 | ``` php
176 | get('hype_mailchimp');
178 | $data = $mc->getCampaign()
179 | ->setCi('1088b4ed65')
180 | ->send();
181 |
182 | var_dump($data);
183 | ?>
184 | ```
185 |
186 | ###Subscribe new user to list
187 | ``` php
188 | get('hype_mailchimp');
190 | $data = $mc->getList()
191 | ->subscribe('moneky@suitMonkry.com');
192 | var_dump($data);
193 | ?>
194 | ```
195 | **Note** that the user will be subscribed to the default list set in `config.yml`.
196 | If you want to change the list for this time only, you can use
197 | ``` php
198 | get('hype_mailchimp');
200 | $data = $mc->getList()
201 | ->setListId('xxxxxxx')
202 | ->addMerge_vars(
203 | array(
204 | 'mc_notes' => 'test notes'
205 | ))
206 | ->subscribe('moneky@suitMonkry.com');
207 | ?>
208 | ```
209 |
--------------------------------------------------------------------------------
/Mailchimp/Methods/MCTemplate.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 |
12 | namespace Hype\MailchimpBundle\Mailchimp\Methods;
13 |
14 | use Hype\MailchimpBundle\Mailchimp\RestClient,
15 | Hype\MailchimpBundle\Mailchimp\MailchimpAPIException;
16 |
17 | class MCTemplate extends RestClient {
18 |
19 | protected $templateId=null;
20 |
21 | /**
22 | * Set template id
23 | * @param type $templateId
24 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCTemplate
25 | */
26 | public function setTemplateId($templateId) {
27 | $this->templateId = $templateId;
28 | return $this;
29 | }
30 |
31 | /**
32 | * Create a new user template, NOT campaign content. These templates can then be applied while creating campaigns.
33 | *
34 | * @link http://apidocs.mailchimp.com/api/2.0/templates/add.php
35 | * @param string $name the name for the template - names must be unique and a max of 50 bytes
36 | * @param string $html a string specifying the entire template to be created. This is NOT campaign conten
37 | * @param int $folderId optional the folder to put this template in.
38 | * @return int template_id on success
39 | * @throws MailchimpAPIException
40 | */
41 | public function add($name, $html, $folderId = null) {
42 | $payload = array(
43 | 'name' => $name,
44 | 'html' => $html,
45 | 'folder_id' => $folderId
46 | );
47 | $apiCall = 'templates/add';
48 | $data = $this->requestMonkey($apiCall, $payload);
49 | $data = json_decode($data,true);
50 | if (isset($data['error']))
51 | throw new MailchimpAPIException($data);
52 | else
53 | return isset($data['template_id']) ? $data['template_id'] : false;
54 | }
55 |
56 | /**
57 | * Retrieve various templates available in the system, allowing some thing similar to our template gallery to be created.
58 | *
59 | * @link http://apidocs.mailchimp.com/api/2.0/templates/list.php
60 | * @param array $types optional the types of templates to return
61 | * @param array $filters optional options to control how inactive templates are returned, if at all
62 | * @return array
63 | * @throws MailchimpAPIException
64 | */
65 | public function listAll($types = array(), $filters = array()) {
66 | $payload = array(
67 | 'types' => $types,
68 | 'filters' => $filters
69 | );
70 | $apiCall = 'templates/list';
71 | $data = $this->requestMonkey($apiCall, $payload);
72 | $data = json_decode($data,true);
73 | if (isset($data['error']))
74 | throw new MailchimpAPIException($data);
75 | else
76 | return $data;
77 | }
78 |
79 | /**
80 | * Delete (deactivate) a user template
81 | *
82 | * @link http://apidocs.mailchimp.com/api/2.0/templates/del.php
83 | * @return boolean true on success
84 | * @throws MailchimpAPIException
85 | */
86 | public function del() {
87 | $payload = array(
88 | 'template_id' => $this->templateId
89 | );
90 | $apiCall = 'templates/del';
91 | $data = $this->requestMonkey($apiCall, $payload);
92 | $data = json_decode($data,true);
93 | if (isset($data['error']))
94 | throw new MailchimpAPIException($data);
95 | else
96 | return true;
97 | }
98 |
99 | /**
100 | * Pull details for a specific template to help support editing
101 | *
102 | * @link http://apidocs.mailchimp.com/api/2.0/templates/info.php
103 | * @param string $type optional the template type to load - one of 'user', 'gallery', 'base', defaults to user.
104 | * @return array
105 | * @throws MailchimpAPIException
106 | */
107 | public function info($type = 'user') {
108 | $payload = array(
109 | 'template_id' => $this->templateId,
110 | 'type' => $type
111 | );
112 | $apiCall = 'templates/info';
113 | $data = $this->requestMonkey($apiCall, $payload);
114 | $data = json_decode($data,true);
115 | if (isset($data['error']))
116 | throw new MailchimpAPIException($data);
117 | else
118 | return $data;
119 | }
120 |
121 | /**
122 | * Undelete (reactivate) a user template
123 | *
124 | * @link http://apidocs.mailchimp.com/api/2.0/templates/undel.php
125 | * @return boolean true on success
126 | * @throws MailchimpAPIException
127 | */
128 | public function undel() {
129 | $payload = array(
130 | 'template_id' => $this->templateId
131 | );
132 | $apiCall = 'templates/undel';
133 | $data = $this->requestMonkey($apiCall, $payload);
134 | $data = json_decode($data,true);
135 | if (isset($data['error']))
136 | throw new MailchimpAPIException($data);
137 | else
138 | return true;
139 | }
140 |
141 | /**
142 | * Replace the content of a user template, NOT campaign content.
143 | *
144 | * @link http://apidocs.mailchimp.com/api/2.0/templates/update.php
145 | * @param array $options the values to updates - while both are optional, at least one should be provided. Both can be updated at the same time.
146 | * @return boolean true on success
147 | * @throws MailchimpAPIException
148 | */
149 | public function update( $options = array()) {
150 | $payload = array(
151 | 'template_id' => $this->templateId,
152 | 'values' => $options
153 | );
154 | $apiCall = 'templates/update';
155 | $data = $this->requestMonkey($apiCall, $payload);
156 | $data = json_decode($data,true);
157 | if (isset($data['error']))
158 | throw new MailchimpAPIException($data);
159 | else
160 | return true;
161 | }
162 |
163 | /* * ********************************** Custom functions ***************************** */
164 |
165 | /**
166 | * Delete template by name
167 | *
168 | * @param string $name
169 | * @return boolean on success
170 | * @throws MailchimpAPIException
171 | */
172 | public function delByName($name) {
173 | return $this->del($this->getIdByName($name));
174 | }
175 |
176 | /**
177 | * Get template id by Name
178 | *
179 | * @param type $name name of the template
180 | * @return int|boolean $id template_id or false
181 | */
182 | public function getByName($name) {
183 | $templates = $this->listAll();
184 | if (empty($templates['user']))
185 | return false;
186 | foreach ($templates['user'] as $template) {
187 | if ($template['name'] === $name) {
188 | return $template['id'];
189 | }
190 | }
191 | return false;
192 | }
193 |
194 | }
195 |
--------------------------------------------------------------------------------
/Mailchimp/Methods/MCCampaign.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 |
12 | namespace Hype\MailchimpBundle\Mailchimp\Methods;
13 |
14 | use Hype\MailchimpBundle\Mailchimp\RestClient,
15 | Hype\MailchimpBundle\Mailchimp\MailchimpAPIException;
16 |
17 | class MCCampaign extends RestClient {
18 |
19 | protected $cid = null;
20 |
21 | /**
22 | * set list id
23 | * @param string $listId
24 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCList
25 | */
26 | public function setListId($listId) {
27 | $this->listId = $listId;
28 | return $this;
29 | }
30 |
31 | /**
32 | * Set Campgain id
33 | * @param int $cid
34 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCCampaign
35 | */
36 | public function setCi($cid) {
37 | $this->cid = $cid;
38 | return $this;
39 | }
40 |
41 | /**
42 | * Create a new draft campaign to send. You can not have more than 32,000 campaigns in your account.
43 | *
44 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/create.php
45 | * @param string $type the Campaign Type has to be one of "regular", "plaintext", "absplit", "rss", "auto"
46 | * @param array $options
47 | * @param array $content
48 | * @param array $segment_opts
49 | * @param array $type_opts
50 | * @return array Campaign data
51 | * @throws \Exception
52 | * @throws MailchimpAPIException
53 | */
54 | public function create($type, $options = array(), $content = array(), $segment_opts = array(), $type_opts = array()) {
55 | if (!in_array($type, array("regular", "plaintext", "absplit", "rss", "auto"))) {
56 | throw new \Exception('the Campaign Type has to be one of "regular", "plaintext", "absplit", "rss", "auto" ');
57 | }
58 | $payload = array(
59 | 'type' => $type,
60 | 'options' => $options,
61 | 'content' => $content,
62 | 'segment_opts' => $segment_opts,
63 | 'type_opts' => $type_opts
64 | );
65 | $apiCall = 'campaigns/create';
66 | $data = $this->requestMonkey($apiCall, $payload);
67 | $data = json_decode($data, true);
68 | if (isset($data['error']))
69 | throw new MailchimpAPIException($data);
70 | else
71 | return isset($data) ? $data : false;
72 | }
73 |
74 | /**
75 | * Get the content (both html and text) for a campaign either as it would appear in the campaign archive or as the raw, original content
76 | *
77 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/content.php
78 | * @param array $options
79 | * @return array Campaign content
80 | * @throws MailchimpAPIException
81 | */
82 | public function content($options = array()) {
83 | $payload = array(
84 | 'cid' => $this->cid,
85 | 'options' => $options
86 | );
87 | $apiCall = 'campaigns/content';
88 | $data = $this->requestMonkey($apiCall, $payload);
89 | $data = json_decode($data, true);
90 | if (isset($data['error']))
91 | throw new MailchimpAPIException($data);
92 | else
93 | return isset($data) ? $data : false;
94 | }
95 |
96 | /**
97 | * Get the list of campaigns and their details matching the specified filters
98 | *
99 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/list.php
100 | * @param array $filters
101 | * @param int $start start page
102 | * @param int $limit limit
103 | * @param string $sort_field
104 | * @param string $sort_dir
105 | * @return array
106 | * @throws \Exception
107 | * @throws MailchimpAPIException
108 | */
109 | public function get($filters = array(), $start = 0, $limit = 25, $sort_field = 'create_time', $sort_dir = "DESC") {
110 | if (!in_array(strtolower($sort_field), array("create_time", "send_time", "title", "subject")))
111 | throw new \Exception('sort_field has to be one of "create_time", "send_time", "title", "subject" ');
112 | if (!in_array(strtoupper($sort_dir), array("ASC", "DESC")))
113 | throw new \Exception('sort_dir has to be one of "ASC", "DESC" ');
114 | $payload = array(
115 | 'filters' => $filters,
116 | 'start' => $start,
117 | 'limit' => $limit,
118 | 'sort_field' => $sort_field,
119 | 'sort_dir' => $sort_dir
120 | );
121 | $apiCall = 'campaigns/list';
122 | $data = $this->requestMonkey($apiCall, $payload);
123 | $data = json_decode($data, true);
124 | if (isset($data['error']))
125 | throw new MailchimpAPIException($data);
126 | else
127 | return isset($data) ? $data : false;
128 | }
129 |
130 | /**
131 | * Delete a campaign. Seriously, "poof, gone!" - be careful! Seriously, no one can undelete these.
132 | *
133 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/delete.php
134 | * @return boolean
135 | * @throws MailchimpAPIException
136 | */
137 | public function del() {
138 | $payload = array(
139 | 'cid' => $this->cid
140 | );
141 | $apiCall = 'campaigns/delete';
142 | $data = $this->requestMonkey($apiCall, $payload);
143 | $data = json_decode($data, true);
144 | if (isset($data['error']))
145 | throw new MailchimpAPIException($data);
146 | else
147 | return true;
148 | }
149 |
150 | /**
151 | * Pause an AutoResponder or RSS campaign from sending
152 | *
153 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/pause.php
154 | * @return boolean
155 | * @throws MailchimpAPIException
156 | */
157 | public function pause() {
158 | $payload = array(
159 | 'cid' => $this->cid
160 | );
161 | $apiCall = 'campaigns/pause';
162 | $data = $this->requestMonkey($apiCall, $payload);
163 | $data = json_decode($data, true);
164 | if (isset($data['error']))
165 | throw new MailchimpAPIException($data);
166 | else
167 | return true;
168 | }
169 |
170 | /**
171 | * Returns information on whether a campaign is ready to send and possible issues we may have detected with it - very similar to the confirmation step in the app.
172 | *
173 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/ready.php
174 | * @return array
175 | * @throws MailchimpAPIException
176 | */
177 | public function ready() {
178 | $payload = array(
179 | 'cid' => $this->cid
180 | );
181 | $apiCall = 'campaigns/ready';
182 | $data = $this->requestMonkey($apiCall, $payload);
183 | $data = json_decode($data, true);
184 | if (isset($data['error']))
185 | throw new MailchimpAPIException($data);
186 | else
187 | return isset($data) ? $data : false;
188 | }
189 |
190 | /**
191 | * Replicate a campaign.
192 | *
193 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/replicate.php
194 | * @return boolean
195 | * @throws MailchimpAPIException
196 | */
197 | public function replicate() {
198 | $payload = array(
199 | 'cid' => $this->cid
200 | );
201 | $apiCall = 'campaigns/replicate';
202 | $data = $this->requestMonkey($apiCall, $payload);
203 | $data = json_decode($data, true);
204 | if (isset($data['error']))
205 | throw new MailchimpAPIException($data);
206 | else
207 | return true;
208 | }
209 |
210 | /**
211 | * Resume sending an AutoResponder or RSS campaign
212 | *
213 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/resume.php
214 |
215 | * @return boolean
216 | * @throws MailchimpAPIException
217 | */
218 | public function resume() {
219 | $payload = array(
220 | 'cid' => $this->cid
221 | );
222 | $apiCall = 'campaigns/resume';
223 | $data = $this->requestMonkey($apiCall, $payload);
224 | $data = json_decode($data, true);
225 | if (isset($data['error']))
226 | throw new MailchimpAPIException($data);
227 | else
228 | return true;
229 | }
230 |
231 | /**
232 | * Send a given campaign immediately. For RSS campaigns, this will "start" them.
233 | *
234 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/send.php
235 | * @return boolean
236 | * @throws MailchimpAPIException
237 | */
238 | public function send() {
239 | $payload = array(
240 | 'cid' => $this->cid
241 | );
242 | $apiCall = 'campaigns/send';
243 | $data = $this->requestMonkey($apiCall, $payload);
244 | $data = json_decode($data, true);
245 | if (isset($data['error']))
246 | throw new MailchimpAPIException($data);
247 | else
248 | return true;
249 | }
250 |
251 | /**
252 | * Send a test of this campaign to the provided email addresses
253 | *
254 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/send-test.php
255 | * @param array $test_emails test email list
256 | * @param array $send_type "text" or "html"
257 | * @return boolean true on success
258 | * @throws \Exception
259 | * @throws MailchimpAPIException
260 | */
261 | public function sendTest($test_emails = array(), $send_type = 'html') {
262 | if (!in_array(strtolower($send_type), array("html", "text")))
263 | throw new \Exception('send_type has to be one of "html", "text" ');
264 | $payload = array(
265 | 'cid' => $this->cid,
266 | 'test_emails' => $test_emails,
267 | 'send_type' => $send_type
268 | );
269 | $apiCall = 'campaigns/send-test';
270 | $data = $this->requestMonkey($apiCall, $payload);
271 | $data = json_decode($data, true);
272 | if (isset($data['error']))
273 | throw new MailchimpAPIException($data);
274 | else
275 | return true;
276 | }
277 |
278 | /**
279 | * Get the HTML template content sections for a campaign. Note that this will return very jagged,
280 | * non-standard results based on the template a campaign is using.
281 | * You only want to use this if you want to allow editing template sections in your application.
282 | *
283 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/template-content.php
284 | * @return boolean true on success
285 | * @return array Campaign Template data
286 | * @throws MailchimpAPIException
287 | */
288 | public function templateContent() {
289 | $payload = array(
290 | 'cid' => $this->cid
291 | );
292 | $apiCall = 'campaigns/template-content';
293 | $data = $this->requestMonkey($apiCall, $payload);
294 | $data = json_decode($data, true);
295 | if (isset($data['error']))
296 | throw new MailchimpAPIException($data);
297 | else
298 | return isset($data) ? $data : false;
299 | }
300 |
301 | /**
302 | * Allows one to test their segmentation rules before creating a campaign using them
303 | *
304 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/segment-test.php
305 | * @param array $options optional
306 | * @return int The total number of subscribers matching your segmentation options
307 | * @throws MailchimpAPIException
308 | */
309 | public function segmentTest($options = array()) {
310 | $payload = array(
311 | 'list_id' => $this->listId,
312 | 'options' => $options
313 | );
314 | $apiCall = 'campaigns/segment-test';
315 | $data = $this->requestMonkey($apiCall, $payload);
316 | $data = json_decode($data, true);
317 | if (isset($data['error']))
318 | throw new MailchimpAPIException($data);
319 | else
320 | return $data['total'];
321 | }
322 |
323 | /**
324 | * Schedule a campaign to be sent in the future
325 | *
326 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/schedule.php
327 | * @param string $schedule_time
328 | * @param string $schedule_time_b
329 | * @return boolean true on success
330 | * @throws MailchimpAPIException
331 | */
332 | public function schedule($schedule_time, $schedule_time_b = null) {
333 | $payload = array(
334 | 'cid' => $this->cid,
335 | 'schedule_time' => $schedule_time,
336 | 'schedule_time_b' => $schedule_time_b
337 | );
338 | $apiCall = 'campaigns/schedule';
339 | $data = $this->requestMonkey($apiCall, $payload);
340 | $data = json_decode($data, true);
341 | if (isset($data['error']))
342 | throw new MailchimpAPIException($data);
343 | else
344 | return true;
345 | }
346 |
347 | /**
348 | * Schedule a campaign to be sent in batches sometime in the future. Only valid for "regular" campaigns
349 | *
350 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/schedule-batch.php
351 | * @param string $schedule_time
352 | * @param int $num_batches
353 | * @param int $stagger_mins
354 | * @return boolean true on success
355 | * @throws MailchimpAPIException
356 | */
357 | public function scheduleBatch($schedule_time, $num_batches = 2, $stagger_mins = 5) {
358 | $payload = array(
359 | 'cid' => $this->cid,
360 | 'schedule_time' => $schedule_time,
361 | 'num_batches' => $num_batches,
362 | 'stagger_mins' => $stagger_mins
363 | );
364 | $apiCall = 'campaigns/schedule-batch';
365 | $data = $this->requestMonkey($apiCall, $payload);
366 | $data = json_decode($data, true);
367 | if (isset($data['error']))
368 | throw new MailchimpAPIException($data);
369 | else
370 | return true;
371 | }
372 |
373 | /**
374 | * Unschedule a campaign that is scheduled to be sent in the future
375 | *
376 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/unschedule.php
377 | * @return boolean true on success
378 | * @throws MailchimpAPIException
379 | */
380 | public function unschedule() {
381 | $payload = array(
382 | 'cid' => $this->cid
383 | );
384 | $apiCall = 'campaigns/unschedule';
385 | $data = $this->requestMonkey($apiCall, $payload);
386 | $data = json_decode($data, true);
387 | if (isset($data['error']))
388 | throw new MailchimpAPIException($data);
389 | else
390 | return true;
391 | }
392 |
393 | /**
394 | * Update just about any setting besides type for a campaign that has not been sent
395 | *
396 | * @link http://apidocs.mailchimp.com/api/2.0/campaigns/update.php
397 | * @param string $id the Campaign Id to update
398 | * @param string $name parameter name
399 | * @param string $value parameter value
400 | * @return boolean true on success
401 | * @throws MailchimpAPIException
402 | */
403 | public function update($name, $value) {
404 | $payload = array(
405 | 'cid' => $this->cid,
406 | 'name' => $name,
407 | 'value' => $value
408 | );
409 | $apiCall = 'campaigns/update';
410 | $data = $this->requestMonkey($apiCall, $payload);
411 | $data = json_decode($data, true);
412 | if (isset($data['error']))
413 | throw new MailchimpAPIException($data);
414 | else
415 | return true;
416 | }
417 |
418 | }
419 |
--------------------------------------------------------------------------------
/Mailchimp/Methods/MCList.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 |
12 | namespace Hype\MailchimpBundle\Mailchimp\Methods;
13 |
14 | use Hype\MailchimpBundle\Mailchimp\RestClient,
15 | Hype\MailchimpBundle\Mailchimp\MailchimpAPIException,
16 | Buzz\Exception\InvalidArgumentException as InvalidArgumentException;
17 |
18 | class MCList extends RestClient
19 | {
20 |
21 | protected $merge_vars = array();
22 | protected $grouping_id = NULL;
23 | protected $group_name = NULL;
24 |
25 | /**
26 | * set list id
27 | * @param string $listId
28 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCList
29 | */
30 | public function setListId($listId)
31 | {
32 | $this->listId = $listId;
33 | return $this;
34 | }
35 |
36 | /**
37 | * set grouping id
38 | * @param int $grouping_id grouping id
39 | * @return \Hype\MailchimpBundle\Mailchimp\Methods\MCList
40 | */
41 | public function setGrouping_id($grouping_id)
42 | {
43 | $this->grouping_id = $grouping_id;
44 | return $this;
45 | }
46 |
47 | /**
48 | * Add to merge vars array
49 | *
50 | * @param mix $merge_vars
51 | */
52 | public function addMerge_vars($merge_vars) {
53 | $this->merge_vars=array_merge($this->merge_vars,$merge_vars);
54 | return $this;
55 | }
56 |
57 | /**
58 | * set to merge vars
59 | * @param mix $merge_vars
60 | */
61 | public function setMerge_vars($merge_vars)
62 | {
63 | $this->merge_vars = $merge_vars;
64 | return $this;
65 | }
66 |
67 | /**
68 | * Get all email addresses that complained about a campaign sent to a list
69 | *
70 | * @link http://apidocs.mailchimp.com/api/2.0/lists/abuse-reports.php
71 | * @param int $start
72 | * @param int $limit
73 | * @param string $string
74 | * @return array
75 | * @throws MailchimpAPIException
76 | */
77 | public function abuseReport($start = 0, $limit = 2000, $string = null)
78 | {
79 | $payload = array(
80 | 'id' => $this->listId,
81 | 'start' => $start,
82 | 'limit' => $limit,
83 | 'string' => $string
84 | );
85 | $apiCall = 'lists/abuse-reports';
86 | $data = $this->requestMonkey($apiCall, $payload);
87 | $data = json_decode($data, true);
88 | if (isset($data['error']))
89 | throw new MailchimpAPIException($data);
90 | else
91 | return isset($data) ? $data : false;
92 | }
93 |
94 | /**
95 | * Access up to the previous 180 days of daily detailed aggregated activity stats for a given list. Does not include AutoResponder activity.
96 | *
97 | * @link http://apidocs.mailchimp.com/api/2.0/lists/activity.php
98 | * @return array
99 | * @throws MailchimpAPIException
100 | */
101 | public function getActivity()
102 | {
103 | $payload = array(
104 | 'id' => $this->listId
105 | );
106 | $apiCall = 'lists/activity';
107 | $data = $this->requestMonkey($apiCall, $payload);
108 | $data = json_decode($data, true);
109 | if (isset($data['error']))
110 | throw new MailchimpAPIException($data);
111 | else
112 | return isset($data) ? $data : false;
113 | }
114 |
115 | /**
116 | * Subscribe a batch of email addresses to a list at once,
117 | * These calls are also long, so be sure you increase your timeout values
118 | *
119 | * @link http://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php
120 | * @param string $batch - array of arrays with ['email','email_type','merge_vars']
121 | * @param boolean $double_optin optional
122 | * @param boolean $update_existing optional
123 | * @param boolean $replace_interests optional
124 | * @return array
125 | * @throws MailchimpAPIException
126 | **/
127 | public function batchSubscribe($batch, $double_optin = true, $update_existing = true, $replace_interests = true) {
128 | $payload = array(
129 | 'id' => $this->listId,
130 | 'batch' => $batch,
131 | 'double_optin' => $double_optin,
132 | 'update_existing' => $update_existing,
133 | 'replace_interests' => $replace_interests,
134 | );
135 | $apiCall = 'lists/batch-subscribe';
136 | $data = $this->requestMonkey($apiCall, $payload);
137 | $data = json_decode($data, true);
138 | if (isset($data['error']))
139 | throw new MailchimpAPIException($data);
140 | else
141 | return isset($data) ? $data : false;
142 | }
143 |
144 | /**
145 | * Unsubscribe a batch of email addresses to a list at once,
146 | * These calls are also long, so be sure you increase your timeout values
147 | *
148 | * @link http://apidocs.mailchimp.com/api/2.0/lists/batch-unsubscribe.php
149 | * @param string $batch - array of arrays with ['email','email_type','merge_vars']
150 | * @param boolean $delete_member optionnal
151 | * @param boolean $send_goodbye optionnal
152 | * @param boolean $send_notify optionnal
153 | * @return array
154 | * @throws MailchimpAPIException
155 | **/
156 | public function batchUnsubscribe($batch, $delete_member = false, $send_goodbye = false, $send_notify = false) {
157 | $payload = array(
158 | 'id' => $this->listId,
159 | 'batch' => $batch,
160 | 'delete_member' => $delete_member,
161 | 'send_goodbye' => $send_goodbye,
162 | 'send_notify' => $send_notify
163 | );
164 | $apiCall = 'lists/batch-unsubscribe';
165 | $data = $this->requestMonkey($apiCall, $payload);
166 | $data = json_decode($data, true);
167 | if (isset($data['error']))
168 | throw new MailchimpAPIException($data);
169 | else
170 | return isset($data) ? $data : false;
171 | }
172 |
173 | /**
174 | * Subscribe an email addresses to a list,
175 | * These calls are also long, so be sure you increase your timeout values
176 | *
177 | * @link http://apidocs.mailchimp.com/api/2.0/lists/subscribe.php
178 | * @param string $email
179 | * @param string $email_type
180 | * @param boolean $double_optin optional
181 | * @param boolean $update_existing optional
182 | * @param boolean $replace_interests optional
183 | * @param boolean $send_welcome optional
184 | * @param string $email_identifier optional can be (email,euid, leid)
185 | * @return array
186 | * @throws MailchimpAPIException
187 | **/
188 | public function subscribe($email_id, $email_type = 'html', $double_optin = true, $update_existing = true, $replace_interests = true, $send_welcome = false, $email_identifier = 'email') {
189 | if (!in_array($email_identifier, array("email", "euid", "leid")))
190 | throw new InvalidArgumentException('email identifier should be one of ("email","euid","leid")');
191 |
192 | $payload = array(
193 | 'id' => $this->listId,
194 | 'email' => array(
195 | $email_identifier => $email_id
196 | ),
197 | 'merge_vars' => $this->merge_vars,
198 | 'email_type' => $email_type,
199 | 'double_optin' => $double_optin,
200 | 'update_existing' => $update_existing,
201 | 'replace_interests' => $replace_interests,
202 | 'send_welcome' => $send_welcome
203 | );
204 |
205 | $apiCall = 'lists/subscribe';
206 | $data = $this->requestMonkey($apiCall, $payload);
207 | $data = json_decode($data, true);
208 | if (isset($data['error']))
209 | throw new MailchimpAPIException($data);
210 | else
211 | return isset($data) ? $data : false;
212 | }
213 |
214 | /**
215 | * Unsubscribe the given email address from the list
216 | *
217 | * @link http://apidocs.mailchimp.com/api/2.0/lists/unsubscribe.php
218 | * @param string $email_id
219 | * @param boolean $delete_member
220 | * @param boolean $send_goodbye
221 | * @param boolean $send_notify
222 | * @param string $email_identifier optional can be (email,euid, leid)
223 | * @return boolean true on success
224 | * @throws InvalidArgumentException
225 | * @throws MailchimpAPIException
226 | */
227 | public function unsubscribe($email_id, $delete_member = false, $send_goodbye = true, $send_notify = true, $email_identifier = 'email')
228 | {
229 |
230 | if (!in_array($email_identifier, array("email", "euid", "leid")))
231 | throw new InvalidArgumentException('email identifier should be one of ("email","euid","leid")');
232 |
233 | $payload = array(
234 | 'id' => $this->listId,
235 | 'email' => array(
236 | $email_identifier => $email_id
237 | ),
238 | 'delete_member' => $delete_member,
239 | 'send_goodbye' => $send_goodbye,
240 | 'send_notify' => $send_notify
241 | );
242 |
243 | $apiCall = 'lists/unsubscribe';
244 | $data = $this->requestMonkey($apiCall, $payload);
245 | $data = json_decode($data, true);
246 | if (isset($data['error']))
247 | throw new MailchimpAPIException($data);
248 | else
249 | return true;
250 | }
251 |
252 | /**
253 | * Get all the information for particular members of a list
254 | *
255 | * @link http://apidocs.mailchimp.com/api/2.0/lists/member-info.php
256 | * @param mix $email_id email id or ids array of emails or string
257 | * @param string $email_identifier optional can be (email,euid, leid)
258 | * @return array
259 | * @throws InvalidArgumentException
260 | */
261 | public function memberInfo($email_id, $email_identifier = 'email')
262 | {
263 | if (!in_array($email_identifier, array("email", "euid", "leid")))
264 | throw new InvalidArgumentException('email identifier should be one of ("email","euid","leid")');
265 | $email_ids = array();
266 | if (is_array($email_id))
267 | {
268 | foreach ($email_id as $email) {
269 | $email_ids[] = array($email_identifier => $email);
270 | }
271 | } else {
272 | $email_ids[] = array($email_identifier => $email_id);
273 | }
274 | $payload = array(
275 | 'id' => $this->listId,
276 | 'emails' => $email_ids
277 | );
278 | $apiCall = 'lists/member-info';
279 | $data = $this->requestMonkey($apiCall, $payload);
280 | $data = json_decode($data, true);
281 | if (isset($data['error']))
282 | throw new MailchimpAPIException($data);
283 | else
284 | return isset($data) ? $data : false;
285 | }
286 |
287 | /**
288 | * Get a list of members for a list
289 | *
290 | * @link http://apidocs.mailchimp.com/api/2.0/lists/members.php
291 | * @param string $status optional 'subscribed', 'unsubscribed', 'cleaned'
292 | * @param array $opts optional
293 | * @return array
294 | * @throws InvalidArgumentException
295 | */
296 | public function members($status = 'subscribed', $opts = null) {
297 |
298 | $payload = array(
299 | 'id' => $this->listId,
300 | 'status' => $status
301 | );
302 |
303 | if (!is_null($opts)) {
304 | $payload['opts'] = $opts;
305 | }
306 |
307 | $apiCall = 'lists/members';
308 | $data = $this->requestMonkey($apiCall, $payload);
309 | $data = json_decode($data, true);
310 | if (isset($data['error']))
311 | throw new MailchimpAPIException($data);
312 | else
313 | return isset($data) ? $data : false;
314 |
315 | }
316 |
317 | /**
318 | * Retrieve all of the lists defined for your user account
319 | *
320 | * @link http://apidocs.mailchimp.com/api/2.0/lists/list.php
321 | * @param array $filters optional - filters to apply to this query
322 | * @param integer $start optional - optional - control paging of lists, start results at this list #, defaults to 1st page of data (page 0)
323 | * @param integer $end optional - optional - control paging of lists, number of lists to return with each call, defaults to 25 (max=100)
324 | * @param string $sort_field optional - optional - "created" (the created date, default) or "web" (the display order in the web app). Invalid values will fall back on "created" - case insensitive.
325 | * @param string $sort_dir optional - optional - "DESC" for descending (default), "ASC" for Ascending. Invalid values will fall back on "created" - case insensitive. Note: to get the exact display order as the web app you'd use "web" and "ASC"
326 | * @return array lists
327 | * @throws MailchimpAPIException
328 | */
329 | public function lists($filters = array(), $start=0, $end=100, $sort_field="created", $sort_dir="DESC")
330 | {
331 |
332 | $payload = array(
333 | 'id' => $this->listId,
334 | 'filters' => $filters,
335 | 'start' => $start,
336 | 'end' => $end,
337 | 'sort_field' => $sort_field,
338 | 'sort_dir' => $sort_dir
339 | );
340 | $apiCall = 'lists/list';
341 | $data = $this->requestMonkey($apiCall, $payload);
342 | $data = json_decode($data, true);
343 | if (isset($data['error']))
344 | throw new MailchimpAPIException($data);
345 | else
346 | return isset($data) ? $data : false;
347 | }
348 |
349 | /**
350 | * Edit the email address, merge fields, and interest groups for a list member. If you are doing a batch update on lots of users, consider using listBatchSubscribe() with the update_existing and possible replace_interests parameter.
351 | *
352 | * @link http://apidocs.mailchimp.com/api/2.0/lists/update-member.php
353 | * @param string $email_id optinal
354 | * @param string $email_type optional can be "html" or "text", defaults "html"
355 | * @param boolean $replace_interests optional
356 | * @param string $email_identifier optional can be (email,euid, leid)
357 | * @return array email information (email,euid, leid)
358 | * @throws InvalidArgumentException
359 | * @throws MailchimpAPIException
360 | */
361 | public function updateMember($email_id, $email_type = 'html', $replace_interests = true, $email_identifier = 'email')
362 | {
363 | if (!in_array($email_identifier, array("email", "euid", "leid")))
364 | throw new InvalidArgumentException('email identifier should be one of ("email","euid","leid")');
365 |
366 | $payload = array(
367 | 'id' => $this->listId,
368 | 'email' => array(
369 | $email_identifier => $email_id
370 | ),
371 | 'merge_vars' => $this->merge_vars,
372 | 'email_type' => $email_type,
373 | 'replace_interests' => $replace_interests
374 | );
375 |
376 | $apiCall = 'lists/update-member';
377 | $data = $this->requestMonkey($apiCall, $payload);
378 | $data = json_decode($data, true);
379 | if (isset($data['error']))
380 | throw new MailchimpAPIException($data);
381 | else
382 | return isset($data) ? $data : false;
383 | }
384 |
385 | /**
386 | * Get the list of interest groupings for a given list, including the label, form information, and included groups for each
387 | *
388 | * @link http://apidocs.mailchimp.com/api/2.0/lists/interest-groupings.php
389 | * @param bool $count optional wether to get subscriber count or not
390 | * @return array all groups information for specific list
391 | * @throws MailchimpAPIException
392 | */
393 | public function interestGroupings($count = null)
394 | {
395 |
396 | $payload = array(
397 | 'id' => $this->listId,
398 | 'count' => $count
399 | );
400 |
401 | $apiCall = 'lists/interest-groupings';
402 | $data = $this->requestMonkey($apiCall, $payload);
403 | $data = json_decode($data, true);
404 | if (isset($data['error']))
405 | throw new MailchimpAPIException($data);
406 | else
407 | return isset($data) ? $data : false;
408 | }
409 |
410 | /**
411 | * Add a new Interest Grouping - if interest groups for the List are not yet enabled, adding the first grouping will automatically turn them on.
412 | *
413 | * @link http://apidocs.mailchimp.com/api/2.0/lists/interest-grouping-add.php
414 | * @param string $name the interest grouping to add - grouping names must be unique
415 | * @param string $type The type of the grouping to add - one of "checkboxes", "hidden", "dropdown", "radio"
416 | * @param array $groups The lists of initial group names to be added - at least 1 is required and the names must be unique within a grouping. If the number takes you over the 60 group limit
417 | * @return array contains id of the new group
418 | * @throws MailchimpAPIException
419 | */
420 | public function addInterestGroupings($name, $type, array $groups)
421 | {
422 |
423 | $payload = array(
424 | 'id' => $this->listId,
425 | 'name' => $name,
426 | 'type' => $type,
427 | 'groups' => $groups
428 | );
429 |
430 | $apiCall = 'lists/interest-grouping-add';
431 | $data = $this->requestMonkey($apiCall, $payload);
432 | $data = json_decode($data, true);
433 | if (isset($data['error']))
434 | throw new MailchimpAPIException($data);
435 | else
436 | return isset($data) ? $data : false;
437 | }
438 |
439 | /**
440 | * Delete an existing Interest Grouping - this will permanently delete all contained interest groups and will remove those selections from all list members
441 | *
442 | * @link http://apidocs.mailchimp.com/api/2.0/lists/interest-grouping-del.php
443 | * @param int $group_id optional the interest grouping id
444 | * @return boolean true on success
445 | * @throws MailchimpAPIException
446 | */
447 | public function delInterestGrouping($group_id = false)
448 | {
449 |
450 | $payload = array(
451 | 'grouping_id' => (FALSE === $group_id) ? $this->grouping_id : $group_id
452 | );
453 |
454 | $apiCall = 'lists/interest-grouping-del';
455 | $data = $this->requestMonkey($apiCall, $payload);
456 | $data = json_decode($data, true);
457 | if (isset($data['error']))
458 | throw new MailchimpAPIException($data);
459 | else
460 | return true;
461 | }
462 |
463 | /**
464 | * Update an existing Interest Grouping
465 | *
466 | * @link http://apidocs.mailchimp.com/api/2.0/lists/interest-grouping-update.php
467 | * @param string $name The name of the field to update - either "name" or "type". Groups within the grouping should be manipulated using the standard listInterestGroup* methods
468 | * @param string $value The new value of the field. Grouping names must be unique - only "hidden" and "checkboxes" grouping types can be converted between each other.
469 | * @param int $group_id optional unless not has been set before
470 | * @return boolean true on success
471 | * @throws MailchimpAPIException
472 | */
473 | public function updateInterestGrouping($name, $value, $group_id = false)
474 | {
475 |
476 | $payload = array(
477 | 'grouping_id' => (FALSE === $group_id) ? $this->grouping_id : $group_id,
478 | 'name' => $name,
479 | 'value' => $value
480 | );
481 |
482 | $apiCall = 'lists/interest-grouping-update';
483 | $data = $this->requestMonkey($apiCall, $payload);
484 | $data = json_decode($data, true);
485 | if (isset($data['error']))
486 | throw new MailchimpAPIException($data);
487 | else
488 | return true;
489 | }
490 |
491 | /**
492 | * Add a single Interest Group - if interest groups for the List are not yet enabled, adding the first group will automatically turn them on.
493 | *
494 | * @link http://apidocs.mailchimp.com/api/2.0/lists/interest-group-add.php
495 | * @param string $name the interest group to add - group names must be unique within a grouping
496 | * @param int $group_id optional The grouping to add the new group to - get using listInterestGrouping() . If not supplied, the first grouping on the list is used.
497 | * @return boolean true on success
498 | * @throws MailchimpAPIException
499 | */
500 | public function addInterestGroup($name, $group_id = NULL)
501 | {
502 |
503 | $payload = array(
504 | 'id' => $this->listId,
505 | 'group_name' => $name,
506 | 'grouping_id' => (NULL === $group_id) ? $this->grouping_id : $group_id,
507 | );
508 |
509 | $apiCall = 'lists/interest-group-add';
510 | $data = $this->requestMonkey($apiCall, $payload);
511 | $data = json_decode($data, true);
512 | if (isset($data['error']))
513 | throw new MailchimpAPIException($data);
514 | else
515 | return true;
516 | }
517 |
518 | /**
519 | * Change the name of an Interest Group
520 | *
521 | * @link http://apidocs.mailchimp.com/api/2.0/lists/interest-group-update.php
522 | * @param string $old_name the interest group name to be changed
523 | * @param string $new_name the new interest group name to be set
524 | * @param int $grouping_id optional The grouping to delete the group from If not supplied, the first grouping on the list is used.
525 | * @return boolean true on success
526 | * @throws MailchimpAPIException
527 | */
528 | public function updateInterestGroup($old_name, $new_name, $grouping_id = NULL)
529 | {
530 |
531 | $payload = array(
532 | 'id' => $this->listId,
533 | 'old_name' => $old_name,
534 | 'new_name' => $new_name,
535 | 'grouping_id' => (NULL === $grouping_id) ? $this->grouping_id : $grouping_id
536 | );
537 |
538 | $apiCall = 'lists/interest-group-update';
539 | $data = $this->requestMonkey($apiCall, $payload);
540 | $data = json_decode($data, true);
541 | if (isset($data['error']))
542 | throw new MailchimpAPIException($data);
543 | else
544 | return true;
545 | }
546 |
547 | /**
548 | * Delete a single Interest Group - if the last group for a list is deleted, this will also turn groups for the list off.
549 | *
550 | * @link http://apidocs.mailchimp.com/api/2.0/lists/interest-group-del.php
551 | * @param string $name the name of interest group to delete
552 | * @param int $grouping_id optional The grouping to delete the group from. If not supplied, the first grouping on the list is used
553 | * @return boolean true on success
554 | * @throws MailchimpAPIException
555 | */
556 | public function delInterestGroup($name, $grouping_id = NULL)
557 | {
558 |
559 | $payload = array(
560 | 'id' => $this->listId,
561 | 'group_name' => $name,
562 | 'grouping_id' => (NULL === $grouping_id) ? $this->grouping_id : $grouping_id,
563 | );
564 |
565 | $apiCall = 'lists/interest-group-del';
566 | $data = $this->requestMonkey($apiCall, $payload);
567 | $data = json_decode($data, true);
568 | if (isset($data['error']))
569 | throw new MailchimpAPIException($data);
570 | else
571 | return true;
572 | }
573 |
574 | /**
575 | * Save a segment against a list for later use. - no limit
576 | * After creating the segment, add members with addMembersStaticSegment
577 | * @link http://apidocs.mailchimp.com/api/2.0/lists/static-segment-members-add.php
578 | *
579 | * @param $name - Name of segment
580 | * @return bool/int - ID of new segment
581 | * @throws MailchimpAPIException
582 | */
583 | public function addStaticSegment($name) {
584 | $payload = array(
585 | 'id' => $this->listId,
586 | 'name' => $name,
587 | );
588 |
589 | $apiCall = 'lists/static-segment-add';
590 | $data = $this->requestMonkey($apiCall, $payload);
591 | $data = json_decode($data, true);
592 |
593 | if (isset($data['error']))
594 | throw new MailchimpAPIException($data);
595 | else
596 | return isset($data['id']) ? $data['id'] : false;
597 | }
598 |
599 | /**
600 | * Save members to a static segment
601 | * @link http://apidocs.mailchimp.com/api/2.0/lists/static-segment-members-add.php
602 | *
603 | * @param $seg_id
604 | * @param $batch - array of emails and uuid
605 | * @return bool
606 | * @throws MailchimpAPIException
607 | */
608 | public function addMembersStaticSegment($seg_id, $batch) {
609 | $payload = array(
610 | 'id' => $this->listId,
611 | 'seg_id' => $seg_id,
612 | 'batch' => $batch
613 | );
614 |
615 | $apiCall = 'lists/static-segment-members-add';
616 | $data = $this->requestMonkey($apiCall, $payload);
617 | $data = json_decode($data, true);
618 |
619 | if (isset($data['error']))
620 | throw new MailchimpAPIException($data);
621 | else
622 | return isset($data) ? $data : false;
623 | }
624 |
625 | /**
626 | * Retrieve all of the Static Segments for a list.
627 | * @link http://apidocs.mailchimp.com/api/2.0/lists/static-segments.php
628 | *
629 | * @return bool|mixed
630 | * @throws \Hype\MailchimpBundle\Mailchimp\MailchimpAPIException
631 | */
632 | public function listStaticSegments() {
633 | $payload = array(
634 | 'id' => $this->listId,
635 | );
636 |
637 | $apiCall = 'lists/static-segments';
638 | $data = $this->requestMonkey($apiCall, $payload);
639 | $data = json_decode($data, true);
640 |
641 | if (isset($data['error']))
642 | throw new MailchimpAPIException($data);
643 | else
644 | return isset($data) ? $data : false;
645 | }
646 |
647 | /**
648 | * Retrieve all of Segments for a list.
649 | * @link https://apidocs.mailchimp.com/api/2.0/lists/segments.php
650 | *
651 | * @return bool|mixed
652 | * @throws \Hype\MailchimpBundle\Mailchimp\MailchimpAPIException
653 | */
654 | public function listSegments() {
655 | $payload = array(
656 | 'id' => $this->listId,
657 | );
658 |
659 | $apiCall = 'lists/segments';
660 | $data = $this->requestMonkey($apiCall, $payload);
661 | $data = json_decode($data, true);
662 |
663 | if (isset($data['error']))
664 | throw new MailchimpAPIException($data);
665 | else
666 | return isset($data) ? $data : false;
667 | }
668 |
669 | /**
670 | * Test a segment and return number of matching subscribers.
671 | * @link https://apidocs.mailchimp.com/api/2.0/lists/segment-test.php
672 | *
673 | * @param $options = array of segment options
674 | * @return bool|mixed
675 | * @throws \Hype\MailchimpBundle\Mailchimp\MailchimpAPIException
676 | */
677 | public function segmentTest($options = array()) {
678 | $payload = array(
679 | 'list_id' => $this->listId,
680 | 'options' => $options
681 | );
682 |
683 | $apiCall = 'lists/segment-test';
684 | $data = $this->requestMonkey($apiCall, $payload);
685 | $data = json_decode($data, true);
686 |
687 | if (isset($data['error']))
688 | throw new MailchimpAPIException($data);
689 | else
690 | return isset($data) ? $data : false;
691 | }
692 |
693 | }
694 |
--------------------------------------------------------------------------------