├── Resources ├── doc │ └── index.rst ├── config │ ├── schema │ │ └── mailchimp-1.0.xsd │ └── services.xml └── meta │ └── LICENSE ├── .gitignore ├── MZMailChimpBundle.php ├── phpunit.xml.dist ├── composer.json ├── .travis.yml ├── Tests ├── DependencyInjection │ ├── ConfigurationTest.php │ └── MZMailChimpExtensionTest.php └── Services │ ├── MailChimpTest.php │ └── Methods │ └── MCExportTest.php ├── Services ├── Methods │ ├── MCExport.php │ ├── MCEcommerce.php │ ├── MCList.php │ └── MCCampaign.php ├── HttpClient.php └── MailChimp.php ├── DependencyInjection ├── Configuration.php └── MZMailChimpExtension.php └── README.md /Resources/doc/index.rst: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | .DS_Store -------------------------------------------------------------------------------- /MZMailChimpBundle.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class MZMailChimpBundle extends Bundle 13 | { 14 | } 15 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | ./Tests 8 | 9 | 10 | 11 | 12 | 13 | ./ 14 | 15 | ./Resources 16 | ./Tests 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Resources/config/schema/mailchimp-1.0.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mlpz/mailchimp-bundle", 3 | "type": "symfony-bundle", 4 | "description": "mailchimp bundle", 5 | "keywords": ["mailchimp","api", "newsletter"], 6 | "homepage": "http://www.mlpz.mp", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Miguel Perez", 11 | "email": "miguel@mlpz.mp" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.2", 16 | "symfony/framework-bundle": "2.*", 17 | "kriswallsmith/buzz":">=0.7", 18 | "ext-curl": "*" 19 | }, 20 | "autoload": { 21 | "psr-0": { "MZ\\MailChimpBundle": "" } 22 | }, 23 | "target-dir": "MZ/MailChimpBundle" 24 | } 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - hhvm 9 | 10 | env: 11 | - SYMFONY_VERSION=2.3.* 12 | 13 | matrix: 14 | allow_failures: 15 | - env: SYMFONY_VERSION=dev-master 16 | - php: hhvm 17 | include: 18 | - php: 5.5 19 | env: SYMFONY_VERSION=2.0.* 20 | - php: 5.5 21 | env: SYMFONY_VERSION=2.1.* 22 | - php: 5.5 23 | env: SYMFONY_VERSION=2.2.* 24 | - php: 5.5 25 | env: SYMFONY_VERSION=2.4.* 26 | - php: 5.5 27 | env: SYMFONY_VERSION=2.5.* 28 | - php: 5.5 29 | env: SYMFONY_VERSION=dev-master 30 | 31 | before_install: 32 | - composer self-update 33 | 34 | install: 35 | - composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source 36 | 37 | script: 38 | - phpunit 39 | -------------------------------------------------------------------------------- /Resources/config/services.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | null 9 | null 10 | true 11 | MZ\MailChimpBundle\Services\MailChimp 12 | 13 | 14 | 15 | 16 | %mz_mail_chimp.api_key% 17 | %mz_mail_chimp.default_list% 18 | %mz_mail_chimp.ssl% 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Tests/DependencyInjection/ConfigurationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\Tests\DependencyInjection; 13 | 14 | use MZ\MailChimpBundle\DependencyInjection\Configuration; 15 | 16 | /** 17 | * Test Configuration 18 | * 19 | * @author Miguel Perez 20 | */ 21 | class ConfigurationTest extends \PHPUnit_Framework_TestCase 22 | { 23 | 24 | /** 25 | * Test get config tree 26 | * 27 | * @covers MZ\MailChimpBundle\DependencyInjection\Configuration::getConfigTreeBuilder 28 | */ 29 | public function testThatCanGetConfigTreeBuilder() 30 | { 31 | $configuration = new Configuration(); 32 | $this->assertInstanceOf('Symfony\Component\Config\Definition\Builder\TreeBuilder', $configuration->getConfigTreeBuilder()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Resources/meta/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Miguel Perez 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /Services/Methods/MCExport.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\Services\Methods; 13 | 14 | use MZ\MailChimpBundle\Services\HttpClient; 15 | 16 | /** 17 | * Mailchimp Export api 18 | * 19 | * @author Miguel Perez 20 | * @link http://apidocs.mailchimp.com/export/1.0/ 21 | */ 22 | class MCExport extends HttpClient 23 | { 24 | 25 | /** 26 | * Dump members of a list 27 | * 28 | * @param $options options 29 | * @return array 30 | */ 31 | public function DumpList($options = array()) 32 | { 33 | $api = 'export/1.0/list/'; 34 | $payload = array_merge(array('id' => $this->listId), $options); 35 | $data = $this->makeRequest($api, $payload, true); 36 | 37 | $result = preg_split ('/$\R?^/m', $data); 38 | 39 | $headerArray = json_decode($result[0]); 40 | unset($result[0]); 41 | 42 | $data = array(); 43 | foreach ($result as $value) { 44 | $data[] = array_combine($headerArray, json_decode($value)); 45 | } 46 | 47 | return $data; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /DependencyInjection/Configuration.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\DependencyInjection; 13 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; 14 | use Symfony\Component\Config\Definition\ConfigurationInterface; 15 | 16 | /** 17 | * This is the class that validates and merges configuration from your app/config files 18 | * 19 | * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} 20 | */ 21 | class Configuration implements ConfigurationInterface 22 | { 23 | /** 24 | * {@inheritDoc} 25 | * @return treeBuilder 26 | */ 27 | public function getConfigTreeBuilder() 28 | { 29 | $treeBuilder = new TreeBuilder(); 30 | $rootNode = $treeBuilder->root('mz_mail_chimp'); 31 | 32 | $rootNode->children() 33 | ->scalarNode('api_key') 34 | ->isRequired() 35 | ->cannotBeEmpty() 36 | ->end() 37 | ->scalarNode('default_list') 38 | ->isRequired() 39 | ->cannotBeEmpty() 40 | ->end() 41 | ->booleanNode('ssl') 42 | ->defaultTrue() 43 | ->end() 44 | ->end(); 45 | 46 | return $treeBuilder; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Tests/Services/MailChimpTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\Tests\Services; 13 | 14 | use MZ\MailChimpBundle\Services\MailChimp; 15 | 16 | /** 17 | * Test mailchimp 18 | * 19 | * @author Miguel Perez 20 | */ 21 | class MailChimpTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Test MailChimp constructor 25 | * 26 | * @covers MZ\MailChimpBundle\Services\MailChimp::__construct 27 | */ 28 | public function testMailChimpConstruct() 29 | { 30 | $mailchimp = new MailChimp('23212312-us2', '12b6c7e6c4'); 31 | 32 | $this->assertEquals('23212312-us2', $mailchimp->getAPIkey()); 33 | $this->assertEquals('12b6c7e6c4', $mailchimp->getListID()); 34 | 35 | } 36 | 37 | /** 38 | * Test MailChimp constructor 39 | * 40 | * @covers MZ\MailChimpBundle\Services\MailChimp::__construct 41 | */ 42 | public function testDatacenter() 43 | { 44 | $mailchimp = new MailChimp('23212312-us2', '12b6c7e6c4', true); 45 | 46 | $this->assertEquals('https://us2.api.mailchimp.com/', $mailchimp->getDatacenter()); 47 | 48 | $mailchimp2 = new MailChimp('23212312-us2', '12b6c7e6c4', false); 49 | 50 | $this->assertEquals('http://us2.api.mailchimp.com/', $mailchimp2->getDatacenter()); 51 | 52 | } 53 | 54 | /** 55 | * Test ListID 56 | * 57 | * @covers MZ\MailChimpBundle\Services\MailChimp::setListID 58 | */ 59 | public function testListID() 60 | { 61 | $mailchimp = new MailChimp('23212312-us2', '12b6c7e6c5'); 62 | 63 | $mailchimp->setListID('12b6c7e6c4'); 64 | $this->assertEquals('12b6c7e6c4', $mailchimp->getListID()); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Services/HttpClient.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\Services; 13 | 14 | use Buzz\Browser, 15 | Buzz\Client\Curl; 16 | 17 | /** 18 | * HTTP client 19 | * 20 | * @author Miguel Perez 21 | */ 22 | class HttpClient 23 | { 24 | protected $dataCenter; 25 | protected $apiKey; 26 | protected $listId; 27 | 28 | /** 29 | * Initializes Http client 30 | * 31 | * @param string $apiKey mailchimp api key 32 | * @param string $listId mailing list id 33 | * @param string $dataCenter mailchimp datacenter 34 | */ 35 | public function __construct($apiKey, $listId, $dataCenter) 36 | { 37 | $this->apiKey = $apiKey; 38 | $this->listId = $listId; 39 | $this->dataCenter = $dataCenter; 40 | } 41 | 42 | /** 43 | * Send API request to mailchimp 44 | * 45 | * @param string $apiCall mailchimp method 46 | * @param string $payload Request information 47 | * @param boolean $export mailchimp export api 48 | * 49 | * @return json 50 | */ 51 | protected function makeRequest($apiCall, $payload, $export = false) 52 | { 53 | $payload['apikey'] = $this->apiKey; 54 | $payload['id'] = $this->listId; 55 | 56 | if ($export) { 57 | $url = $this->dataCenter . $apiCall; 58 | } else { 59 | $url = $this->dataCenter . '1.3/?method=' . $apiCall; 60 | } 61 | $curl = new Curl(); 62 | $curl->setOption(CURLOPT_USERAGENT, 'MZMailChimpBundle'); 63 | 64 | // Set long timeout for Export API to allow for larger responses 65 | if ($export) { 66 | $curl->setOption(CURLOPT_TIMEOUT, 600); 67 | } 68 | 69 | $browser = new Browser($curl); 70 | $response = $browser->post($url, array(), http_build_query($payload)); 71 | 72 | return $response->getContent(); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /DependencyInjection/MZMailChimpExtension.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\DependencyInjection; 13 | use Symfony\Component\DependencyInjection\ContainerBuilder; 14 | use Symfony\Component\Config\FileLocator; 15 | use Symfony\Component\HttpKernel\DependencyInjection\Extension; 16 | use Symfony\Component\DependencyInjection\Loader; 17 | 18 | /** 19 | * This is the class that loads and manages your bundle configuration 20 | * 21 | * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} 22 | */ 23 | class MZMailChimpExtension extends Extension 24 | { 25 | /** 26 | * {@inheritDoc} 27 | */ 28 | public function load(array $configs, ContainerBuilder $container) 29 | { 30 | $configuration = new Configuration(); 31 | $config = $this->processConfiguration($configuration, $configs); 32 | 33 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); 34 | $loader->load('services.xml'); 35 | 36 | foreach (array('api_key', 'default_list', 'ssl') as $attribute) { 37 | if (isset($config[$attribute])) { 38 | $container->setParameter('mz_mail_chimp.' . $attribute, $config[$attribute]); 39 | } 40 | } 41 | } 42 | 43 | /** 44 | * @codeCoverageIgnore 45 | */ 46 | public function getXsdValidationBasePath() 47 | { 48 | return __DIR__ . '/../Resources/config/schema'; 49 | } 50 | 51 | /** 52 | * @codeCoverageIgnore 53 | * @return string 54 | */ 55 | public function getNamespace() 56 | { 57 | return 'http://symfony.com/schema/dic/mz_mailchimp'; 58 | } 59 | 60 | /** 61 | * @codeCoverageIgnore 62 | */ 63 | protected function loadDefaults($container) 64 | { 65 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); 66 | 67 | foreach ($this->resources as $resource) { 68 | $loader->load($resource); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Tests/DependencyInjection/MZMailChimpExtensionTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\Tests\DependencyInjection; 13 | 14 | use MZ\MailChimpBundle\DependencyInjection\MZMailChimpExtension; 15 | 16 | /** 17 | * Test MZMailChimpExtension 18 | * 19 | * @author Miguel Perez 20 | */ 21 | class MZMailChimpExtensionTest extends \PHPUnit_Framework_TestCase 22 | { 23 | 24 | /** 25 | * Test load failed 26 | * 27 | * @covers MZ\MailChimpBundle\DependencyInjection\MZMailChimpExtension::load 28 | */ 29 | public function testLoadFailed() 30 | { 31 | $container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder') 32 | ->disableOriginalConstructor() 33 | ->getMock(); 34 | 35 | $extension = $this->getMockBuilder('MZ\MailChimpBundle\DependencyInjection\MZMailChimpExtension') 36 | ->getMock(); 37 | 38 | $extension->load(array(array()), $container); 39 | } 40 | 41 | /** 42 | * Test setParameters 43 | * 44 | * @covers MZ\MailChimpBundle\DependencyInjection\MZMailChimpExtension::load 45 | */ 46 | public function testLoadSetParameters() 47 | { 48 | $container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder') 49 | ->disableOriginalConstructor() 50 | ->getMock(); 51 | 52 | $parameterBag = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag') 53 | ->disableOriginalConstructor() 54 | ->getMock(); 55 | 56 | $parameterBag->expects($this->any()) 57 | ->method('add'); 58 | 59 | $container->expects($this->any()) 60 | ->method('getParameterBag') 61 | ->will($this->returnValue($parameterBag)); 62 | 63 | $extension = new MZMailChimpExtension(); 64 | $configs = array(array('api_key' => 'foo'), 65 | array('default_list' => 'foo'),); 66 | $extension->load($configs, $container); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Tests/Services/Methods/MCExportTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This source file is subject to the MIT license that is bundled 8 | * with this source code in the file LICENSE. 9 | */ 10 | use MZ\MailChimpBundle\Services\HttpClient; 11 | use MZ\MailChimpBundle\Services\Methods\MCExport; 12 | /** 13 | * Test Mailchimp Export Class 14 | * 15 | * @author Miguel Perez 16 | */ 17 | class MCExportTest extends \PHPUnit_Framework_TestCase 18 | { 19 | public function testDumpList() 20 | { 21 | $expect = array( 22 | 0 => array( 23 | 'Email Address' => 'test@gmail.com', 24 | 'First Name' => 'test', 25 | 'Last Name' => 'test', 26 | 'Skills' => 'Both', 27 | 'testing' => '', 28 | 'MEMBER_RATING' => 2, 29 | 'OPTIN_TIME' => '2012-07-03 21:47:23', 30 | 'OPTIN_IP' => '24.189.148.246', 31 | 'CONFIRM_TIME' => '2012-07-03 21:47:31', 32 | 'CONFIRM_IP' => '24.189.148.246', 33 | 'LATITUDE' => '40.8090000', 34 | 'LONGITUDE' => '-73.9168000', 35 | 'GMTOFF' => '-5', 36 | 'DSTOFF' => '-4', 37 | 'TIMEZONE' => 'America/New_York', 38 | 'CC' => 'US', 39 | 'REGION' => 'NY', 40 | 'LAST_CHANGED' => '2012-07-03 21:47:31' 41 | ) 42 | ); 43 | 44 | $export = $this->getMock('MZ\MailChimpBundle\Services\Methods\MCExport', 45 | array('makeRequest'), 46 | array(), 47 | '', 48 | false, 49 | false, 50 | true); 51 | 52 | $reponse = <<expects($this->any())->method('makeRequest')->will($this->returnValue($reponse)); 57 | $dump = $export->DumpList(); 58 | 59 | $this->assertEquals($expect, $dump); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Services/MailChimp.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\Services; 13 | 14 | /** 15 | * Mailchimp 16 | * 17 | * @author Miguel Perez 18 | */ 19 | class MailChimp 20 | { 21 | 22 | private $apiKey; 23 | private $listId; 24 | private $dataCenter; 25 | 26 | /** 27 | * Initializes MailChimp 28 | * 29 | * @param string $apiKey Mailchimp api key 30 | * @param string $listId Default mailing list id 31 | */ 32 | public function __construct($apiKey, $listId, $ssl = true) 33 | { 34 | trigger_error("API v1 has being deprecated by MailChimp. A new version of this bundle is being work on.", E_USER_DEPRECATED); 35 | 36 | $this->apiKey = $apiKey; 37 | $this->listId = $listId; 38 | 39 | $key = preg_split("/-/", $this->apiKey); 40 | 41 | if($ssl) { 42 | $this->dataCenter ='https://' . $key[1] . '.api.mailchimp.com/'; 43 | }else { 44 | $this->dataCenter ='http://' . $key[1] . '.api.mailchimp.com/'; 45 | } 46 | 47 | if (!function_exists('curl_init')) { 48 | throw new \Exception('This bundle needs the cURL PHP extension.'); 49 | } 50 | } 51 | 52 | /** 53 | * Get Mailchimp api key 54 | * 55 | * @return string 56 | */ 57 | public function getAPIkey() 58 | { 59 | return $this->apiKey; 60 | } 61 | 62 | /** 63 | * Set mailing list id 64 | * 65 | * @param string $listId mailing list id 66 | */ 67 | public function setListID($listId) 68 | { 69 | $this->listId = $listId; 70 | } 71 | 72 | /** 73 | * get mailing list id 74 | * 75 | * @return string $listId 76 | */ 77 | public function getListID() 78 | { 79 | return $this->listId; 80 | } 81 | 82 | /** 83 | * get datacenter 84 | * 85 | * @return string $datacenter 86 | */ 87 | public function getDatacenter() 88 | { 89 | return $this->dataCenter; 90 | } 91 | 92 | /** 93 | * Get List Methods 94 | * 95 | * @return Methods\MCList 96 | */ 97 | public function getList() 98 | { 99 | return new Methods\MCList($this->apiKey, $this->listId, $this->dataCenter); 100 | } 101 | 102 | /** 103 | * Get List Methods 104 | * 105 | * @return Methods\MCCampaign 106 | */ 107 | public function getCampaign() 108 | { 109 | return new Methods\MCCampaign($this->apiKey, $this->listId, $this->dataCenter); 110 | } 111 | 112 | /** 113 | * Get Export API 114 | * 115 | * @return Methods\MCExport 116 | */ 117 | public function getExport() 118 | { 119 | return new Methods\MCExport($this->apiKey, $this->listId, $this->dataCenter); 120 | } 121 | 122 | /** 123 | * Get Ecommerce Methods 124 | * 125 | * @return Methods\MCEcommerce 126 | */ 127 | public function getEcommerce() 128 | { 129 | return new Methods\MCEcommerce($this->apiKey, $this->listId, $this->dataCenter); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /Services/Methods/MCEcommerce.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\Services\Methods; 13 | 14 | use MZ\MailChimpBundle\Services\HttpClient; 15 | 16 | /** 17 | * Mailchimp Ecommerce methods 18 | * 19 | * @author Joona Savolainen 20 | * @link http://apidocs.mailchimp.com/api/1.3/#ecommerce 21 | */ 22 | class MCEcommerce extends HttpClient 23 | { 24 | private $order_id; 25 | private $total = 0; 26 | private $order_date; 27 | private $shipping; 28 | private $tax; 29 | private $store_id; 30 | private $store_name; 31 | private $campaign_id; 32 | private $items = array(); 33 | 34 | /** 35 | * Set Order Id 36 | * 37 | * @param integer $order_id 38 | */ 39 | public function setOrderId($order_id) 40 | { 41 | $this->order_id = $order_id; 42 | } 43 | 44 | /** 45 | * Get Order Id 46 | * 47 | * @return integer 48 | */ 49 | public function getOrderId() 50 | { 51 | return $this->order_id; 52 | } 53 | 54 | /** 55 | * Set total value of the Order 56 | * 57 | * @param double $total 58 | */ 59 | public function setTotal($total) 60 | { 61 | $this->total = $total; 62 | } 63 | 64 | /** 65 | * Get total value of the Order 66 | * 67 | * @return double 68 | */ 69 | public function getTotal() 70 | { 71 | return $this->total; 72 | } 73 | 74 | /** 75 | * Set date of the Order (optional) 76 | * 77 | * @param string $date 78 | */ 79 | public function setOrderDate($date) 80 | { 81 | $this->order_date = $date; 82 | } 83 | 84 | /** 85 | * Get date of the Order 86 | * 87 | * @return string 88 | */ 89 | public function getOrderDate() 90 | { 91 | return $this->order_date; 92 | } 93 | 94 | /** 95 | * Set amount of shipping fees (optional) 96 | * 97 | * @param double $shipping 98 | */ 99 | public function setShipping($shipping) 100 | { 101 | $this->shipping = $shipping; 102 | } 103 | 104 | /** 105 | * Get amount of shipping fees 106 | * 107 | * @return double 108 | */ 109 | public function getShipping() 110 | { 111 | return $this->shipping; 112 | } 113 | 114 | /** 115 | * Set amount of taxes (optional) 116 | * 117 | * @param double $tax 118 | */ 119 | public function setTax($tax) 120 | { 121 | $this->tax = $tax; 122 | } 123 | 124 | /** 125 | * Get amount of taxes 126 | * 127 | * @return double 128 | */ 129 | public function getTax() 130 | { 131 | return $this->tax; 132 | } 133 | 134 | /** 135 | * Set Store Id of the Order 136 | * 137 | * @param string $id 138 | */ 139 | public function setStoreId($id) 140 | { 141 | $this->store_id = $id; 142 | } 143 | 144 | /** 145 | * Get Store Id of the Order 146 | * 147 | * @return double 148 | */ 149 | public function getStoreId() 150 | { 151 | return $this->store_id; 152 | } 153 | 154 | /** 155 | * Set Store name of the Order (optional) 156 | * 157 | * @param string $name 158 | */ 159 | public function setStoreName($name) 160 | { 161 | $this->store_name = $name; 162 | } 163 | 164 | /** 165 | * Get Store name of the Order 166 | * 167 | * @return string 168 | */ 169 | public function getStoreName() 170 | { 171 | return $this->store_name; 172 | } 173 | 174 | /** 175 | * Set MailChimp Campaign Id (mc_cid) of the Order (optional) 176 | * 177 | * @param string $campaign_id 178 | */ 179 | public function setCampaignId($campaign_id) 180 | { 181 | $this->campaign_id = $campaign_id; 182 | } 183 | 184 | /** 185 | * Get MailChimp Campaign Id of the Order 186 | * 187 | * @return type 188 | */ 189 | public function getCampaignId() 190 | { 191 | return $this->campaign_id; 192 | } 193 | 194 | /** 195 | * Adds item to the Order 196 | * 197 | * @param integer $product_id Id Of the item 198 | * @param string $product_name Name of the item 199 | * @param integer $category_id Category Id of the item 200 | * @param string $category_name Category Name of the item 201 | * @param integer $qty Item quantity 202 | * @param double $cost Price of the Item 203 | * @param string|null $sku SKU of the Item (optional) 204 | */ 205 | public function addItem($product_id, $product_name, $category_id, $category_name, $qty, $cost, $sku = null) 206 | { 207 | $item = array( 208 | 'product_id' => $product_id, 209 | 'product_name' => $product_name, 210 | 'category_id' => $category_id, 211 | 'category_name' => $category_name, 212 | 'qty' => $qty, 213 | 'cost' => $cost 214 | ); 215 | 216 | if($sku) { 217 | $item['sku'] = $sku; 218 | } 219 | 220 | $this->items[] = $item; 221 | } 222 | 223 | /** 224 | * Adds new Ecommerce Order 225 | * 226 | * @param string|null $email Email of the subscriber 227 | * @param string|null $email_id Email Id of the subscriber 228 | * @return mixed 229 | */ 230 | public function addOrder($email = null, $email_id = null) 231 | { 232 | $order = array( 233 | 'id' => $this->order_id, 234 | 'total' => $this->total, 235 | 'store_id' => $this->store_id, 236 | 'items' => $this->items 237 | ); 238 | 239 | if($email_id) { 240 | $order['email_id'] = $email_id; 241 | } 242 | else if($email) { 243 | $order['email'] = $email; 244 | } 245 | else { 246 | throw new \Exception("Either email_id or email must be provided"); 247 | } 248 | 249 | if($this->order_date) { 250 | $order['order_date'] = $this->order_date; 251 | } 252 | if($this->shipping) { 253 | $order['shipping'] = $this->shipping; 254 | } 255 | if($this->tax) { 256 | $order['tax'] = $this->tax; 257 | } 258 | if($this->store_name) { 259 | $order['store_name'] = $this->store_name; 260 | } 261 | if($this->campaign_id) { 262 | $order['campaign_id'] = $this->campaign_id; 263 | } 264 | 265 | $payload = array('order' => $order); 266 | 267 | $apiCall = 'ecommOrderAdd'; 268 | $data = $this->makeRequest($apiCall, $payload); 269 | $data = json_decode($data); 270 | 271 | return $data; 272 | } 273 | 274 | /** 275 | * Deletes Ecommerge Order 276 | * 277 | * @param string $store_id 278 | * @param string $order_id 279 | * @return mixed 280 | */ 281 | public function deleteOrder($store_id, $order_id) 282 | { 283 | $payload = array('store_id' => $store_id, 'order_id' => $order_id); 284 | 285 | $apiCall = 'ecommOrderDel'; 286 | $data = $this->makeRequest($apiCall, $payload); 287 | $data = json_decode($data); 288 | 289 | return $data; 290 | } 291 | 292 | /** 293 | * Retrieves Ecommerce Orders for the account 294 | * 295 | * @param integer $start The page number to start at 296 | * @param integer $limit Result limit (max. 500) 297 | * @param \DateTime|null $since Get orders since this time 298 | * @return mixed 299 | */ 300 | public function getOrders($start = 0, $limit = 100, \DateTime $since = null) 301 | { 302 | $payload = array('start' => $start, 'limit' => $limit); 303 | 304 | if($since) { 305 | $since->setTimezone(new \DateTimeZone('Etc/GMT')); 306 | $payload['since'] = $since->format('Y-m-d H:i:s'); 307 | } 308 | 309 | $apiCall = 'ecommOrders'; 310 | $data = $this->makeRequest($apiCall, $payload); 311 | $data = json_decode($data); 312 | 313 | return $data; 314 | } 315 | } 316 | -------------------------------------------------------------------------------- /Services/Methods/MCList.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\Services\Methods; 13 | 14 | use MZ\MailChimpBundle\Services\HttpClient; 15 | 16 | /** 17 | * Mailchimp List method 18 | * 19 | * @author Miguel Perez 20 | * @link http://apidocs.mailchimp.com/api/1.3/#listrelated 21 | */ 22 | class MCList extends HttpClient 23 | { 24 | 25 | private $merge = array(); 26 | private $emailType = 'html'; 27 | private $doubleOptin = true; 28 | private $updateExisting = false; 29 | private $replaceInterests = true; 30 | private $sendWelcome = false; 31 | private $email; 32 | private $mergeVars = array(); 33 | private $deleteMember = true; 34 | private $sendGoodbye = false; 35 | private $sendNotify = false; 36 | 37 | /** 38 | * Set mailchimp merge 39 | * 40 | * @param array $merge subscribe merge 41 | */ 42 | public function setMerge(array $merge) 43 | { 44 | $this->merge = $merge; 45 | } 46 | 47 | /** 48 | * Set mailchimp email type 49 | * 50 | * @param string $emailType string to send email type 51 | */ 52 | public function setEmailType($emailType) 53 | { 54 | $this->emailType = $emailType; 55 | } 56 | 57 | /** 58 | * Set mailchimp double optin 59 | * 60 | * @deprecated due to spelling mistake 61 | * @param boolean $doubleOptin boolean to double optin 62 | */ 63 | public function setDoubleOption($doubleOptin) 64 | { 65 | $this->setDoubleOptin($doubleOptin); 66 | } 67 | 68 | /** 69 | * Set mailchimp double optin 70 | * 71 | * @param boolean $doubleOptin boolean to double optin 72 | */ 73 | public function setDoubleOptin($doubleOptin) 74 | { 75 | $this->doubleOptin = $doubleOptin; 76 | } 77 | 78 | /** 79 | * Set mailchimp update existing 80 | * 81 | * @param boolean $updateExisting boolean to update user 82 | */ 83 | public function setUpdateExisting($updateExisting) 84 | { 85 | $this->updateExisting = $updateExisting; 86 | } 87 | 88 | /** 89 | * Set mailchimp replace interests 90 | * 91 | * @param boolean $replaceInterests boolean to replace intersests 92 | */ 93 | public function setReplaceInterests($replaceInterests) 94 | { 95 | $this->replaceInterests = $replaceInterests; 96 | } 97 | 98 | /** 99 | * Set mailchimp send welcome 100 | * 101 | * @param boolean $sendWelcome boolean to send welcome email 102 | */ 103 | public function SendWelcome($sendWelcome) 104 | { 105 | $this->sendWelcome = $sendWelcome; 106 | } 107 | 108 | /** 109 | * Set mailchimp merge_vars 110 | * 111 | * @param unknown_type $email Old user email 112 | * @param unknown_type $newEmail New user email 113 | * @param unknown_type $groupings User group 114 | * @param unknown_type $optionIP User ip addres 115 | * @param unknown_type $optinTime Subcribe time 116 | * @param unknown_type $mcLocation Use location 117 | */ 118 | public function MergeVars($email = null, $newEmail = null, $groupings = null, $optionIP = null, $optinTime = null, $mcLocation = null) 119 | { 120 | $this->mergeVars['EMAIL'] = $email; 121 | $this->mergeVars['NEW-EMAIL'] = $newEmail; 122 | $this->mergeVars['GROUPINGS'] = $groupings; 123 | $this->mergeVars['OPTIN_IP'] = $optionIP; 124 | $this->mergeVars['OPTIN_TIME'] = $optinTime; 125 | $this->mergeVars['MC_LOCATION'] = $mcLocation; 126 | $this->mergeVars = array_merge($this->mergeVars, $this->merge); 127 | } 128 | 129 | /** 130 | * Set user email 131 | * 132 | * @param string $email user email 133 | */ 134 | 135 | public function setEmail($email) 136 | { 137 | $this->email = $email; 138 | } 139 | 140 | /** 141 | * Subscribe member to list 142 | * 143 | * @param string $email user email 144 | * 145 | * @return boolean 146 | */ 147 | public function Subscribe($email = null) 148 | { 149 | if (!empty($email)) { 150 | $this->email = $email; 151 | } 152 | 153 | $payload = array('email_address' => $this->email, 154 | 'merge_vars' => $this->merge, 'email_type' => $this->emailType, 155 | 'double_optin' => $this->doubleOptin, 156 | 'update_existing' => $this->updateExisting, 157 | 'replace_interests' => $this->replaceInterests, 158 | 'send_welcome' => $this->sendWelcome,); 159 | 160 | $apiCall = 'listSubscribe'; 161 | $data = $this->makeRequest($apiCall, $payload); 162 | $data = json_decode($data); 163 | 164 | return $data; 165 | } 166 | 167 | /** 168 | * Subscribe member to list 169 | * 170 | * @param string $email user email 171 | * 172 | * @return boolean 173 | */ 174 | public function UnSubscribe($email) 175 | { 176 | 177 | $payload = array('email_address' => $email, 178 | 'delete_member' => $this->deleteMember, 179 | 'send_goodbye' => $this->sendGoodbye, 180 | 'send_notify' => $this->sendNotify,); 181 | 182 | $apiCall = 'listUnsubscribe'; 183 | $data = $this->makeRequest($apiCall, $payload); 184 | $data = json_decode($data); 185 | 186 | return $data; 187 | } 188 | 189 | /** 190 | * Update member 191 | * 192 | * @return array 193 | */ 194 | public function UpdateMember(array $vars = array()) 195 | { 196 | $payload = array('email_address' => $this->email, 197 | 'merge_vars' => $vars + $this->mergeVars, 198 | 'email_type' => $this->emailType,); 199 | 200 | $apiCall = 'listUpdateMember'; 201 | $data = $this->makeRequest($apiCall, $payload); 202 | $data = json_decode($data); 203 | return $data; 204 | } 205 | 206 | /** 207 | * Get info about a member 208 | * 209 | * @return object 210 | */ 211 | public function getMemberInfo($email) 212 | { 213 | $payload = array('email_address' => $email); 214 | $apiCall = 'listMemberInfo'; 215 | $data = $this->makeRequest($apiCall, $payload); 216 | return json_decode($data); 217 | } 218 | 219 | /** 220 | * Create new group with subgroups 221 | * 222 | * @param string $name group name 223 | * @param string $type group type 224 | * @param array $groups subgroups 225 | * 226 | * @return int group id 227 | */ 228 | public function listInterestGroupingAdd($name, $type, $groups = array()) 229 | { 230 | $payload = array( 231 | 'name' => $name, 232 | 'type' => $type, 233 | 'groups' => $groups, 234 | ); 235 | $apiCall = 'listInterestGroupingAdd'; 236 | $data = $this->makeRequest($apiCall, $payload); 237 | return json_decode($data); 238 | } 239 | 240 | /** 241 | * create static segment 242 | * @param segment name 243 | * @return segment id 244 | */ 245 | public function listStaticSegmentAdd($name) 246 | { 247 | $payload = array( 248 | 'id' => $this->listId, 249 | 'name' => $name, 250 | ); 251 | $apiCall = 'listStaticSegmentAdd'; 252 | $data = $this->makeRequest($apiCall, $payload); 253 | return json_decode($data); 254 | } 255 | 256 | /** 257 | * add emails to segment 258 | * @param int $seg_id 259 | * @param array $batch 260 | * @return mixed 261 | */ 262 | public function listStaticSegmentMembersAdd($seg_id, $batch = array()) 263 | { 264 | $payload = array( 265 | 'id' => $this->listId, 266 | 'seg_id' => $seg_id, 267 | 'batch' => $batch, 268 | ); 269 | $apiCall = 'listStaticSegmentMembersAdd'; 270 | $data = $this->makeRequest($apiCall, $payload); 271 | return json_decode($data); 272 | } 273 | 274 | /** 275 | * delete emails from segment 276 | * @param int $seg_id 277 | * @param array $batch 278 | * @return mixed 279 | */ 280 | public function listStaticSegmentMembersDel($seg_id, $batch = array()) 281 | { 282 | $payload = array( 283 | 'id' => $this->listId, 284 | 'seg_id' => $seg_id, 285 | 'batch' => $batch, 286 | ); 287 | $apiCall = 'listStaticSegmentMembersDel'; 288 | $data = $this->makeRequest($apiCall, $payload); 289 | return json_decode($data); 290 | } 291 | 292 | 293 | /** 294 | * get all segments 295 | * @return array 296 | */ 297 | public function listStaticSegments() 298 | { 299 | $payload = array( 300 | 'id' => $this->listId, 301 | ); 302 | $apiCall = 'listStaticSegments'; 303 | $data = $this->makeRequest($apiCall, $payload); 304 | return json_decode($data); 305 | } 306 | 307 | 308 | /** 309 | * Get all interest groupings 310 | * @return array 311 | */ 312 | public function listInterestGroupings() 313 | { 314 | $payload = array( 315 | 'id' => $this->listId, 316 | ); 317 | $apiCall = 'listInterestGroupings'; 318 | $data = $this->makeRequest($apiCall, $payload); 319 | return json_decode($data); 320 | } 321 | } 322 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MZMailChimpBundle 2 | Symfony2 bundle for [MailChimp](http://apidocs.mailchimp.com/api/1.3/index.php) API And [Export API](http://apidocs.mailchimp.com/export/1.0/) 3 | 4 | [![Build Status](https://secure.travis-ci.org/miguel250/MZMailChimpBundle.png?branch=master)](http://travis-ci.org/miguel250/MZMailChimpBundle) [![Total Downloads](https://poser.pugx.org/mlpz/mailchimp-bundle/downloads.png)](https://packagist.org/packages/mlpz/mailchimp-bundle) [![Latest Stable Version](https://poser.pugx.org/mlpz/mailchimp-bundle/v/stable.png)](https://packagist.org/packages/mlpz/mailchimp-bundle) 5 | 6 | **License** 7 | 8 | MZMailChimpBundle is licensed under the MIT License - see the `Resources/meta/LICENSE` file for details 9 | 10 | **MailChimp API Method Supported** 11 | 12 | 1. `listSubscribe` 13 | 2. `listUnSubscribe` 14 | 3. `listUpdateMember` 15 | 4. `listInterestGroupingAdd` 16 | 5. `campaignCreate` 17 | 6. `campaignSendTest` 18 | 7. `campaignSendNow` 19 | 8. `listStaticSegmentAdd` 20 | 9. `listStaticSegmentMembersAdd` 21 | 10. `listStaticSegments` 22 | 11. `campaigns` 23 | 12. `campaignStats` 24 | 25 | **MailChimp Export API Method Supported** 26 | 27 | `1. list` 28 | 29 | Need support for a method not on the list submit an [issue](MZMailChimpBundle/issues/new) 30 | 31 | ## Setup 32 | 33 | ### Step 1: Download MZMailChimpBundle using composer 34 | 35 | Add MZMailChimpBundle in your composer.json: 36 | 37 | ```js 38 | { 39 | "require": { 40 | "mlpz/mailchimp-bundle": "dev-master" 41 | } 42 | } 43 | ``` 44 | 45 | Now tell composer to download the bundle by running the command: 46 | 47 | ``` bash 48 | $ php composer.phar update mlpz/mailchimp-bundle 49 | ``` 50 | 51 | Composer will install the bundle to your project's `vendor/mlpz` directory. 52 | 53 | ### Step 2: Enable the bundle 54 | 55 | Enable the bundle in the kernel: 56 | 57 | ``` php 58 | get('MailChimp'); 87 | ``` 88 | 89 | **MailChimp API [Subscribe](http://apidocs.mailchimp.com/api/1.3/listsubscribe.func.php) user to mailing list in a controller** 90 | 91 | ``` php 92 | get('MailChimp'); 94 | 95 | /** 96 | * Change mailing list 97 | * */ 98 | $mailChimp->setListID($id); 99 | 100 | /** 101 | * Get list methods 102 | * */ 103 | $list = $mailChimp->getList(); 104 | 105 | /** 106 | * listSubscribe default Parameters 107 | * */ 108 | $list->setMerge($array); //optional default: null 109 | $list->setEmailType('html'); //optional default: html 110 | $list->setDoubleOptin(true); //optional default : true 111 | $list->setUpdateExisting(false); // optional default : false 112 | $list->setReplaceInterests(true); // optional default : true 113 | $list->SendWelcome(false); // optional default : false 114 | 115 | /** 116 | * Subscribe user to list 117 | * */ 118 | $list->Subscribe($email); //boolean 119 | ``` 120 | 121 | **MailChimp API [Unsubscribe](http://apidocs.mailchimp.com/api/1.3/listunsubscribe.func.php) remove user from mailing list in a controller** 122 | 123 | ``` php 124 | get('MailChimp'); 126 | 127 | /** 128 | * Change mailing list 129 | * */ 130 | $mailChimp->setListID($id); 131 | 132 | /** 133 | * Get list methods 134 | * */ 135 | $list = $mailChimp->getList(); 136 | 137 | /** 138 | * UnSubscribe user from list 139 | * */ 140 | $list->UnSubscribe($email); //boolean 141 | ``` 142 | 143 | **MailChimp API [Update](http://apidocs.mailchimp.com/api/1.3/listupdatemember.func.php) user in a controller** 144 | 145 | ``` php 146 | get('MailChimp'); 148 | $list = $mailChimp->getList(); 149 | $list->setEmail($oldEmail); 150 | $list->MergeVars($newEmail); 151 | 152 | /** 153 | * Update user in mailing list 154 | **/ 155 | $list->UpdateMember(); //boolean 156 | ``` 157 | 158 | **MailChimp API [Interest Grouping Add](http://apidocs.mailchimp.com/api/rtfm/listinterestgroupingadd.func.php) in a controller** 159 | 160 | ``` php 161 | get('MailChimp'); 163 | $list = $mailChimp->getList(); 164 | $list->listInterestGroupingAdd( 165 | $groupTitle, $groupType, 166 | array($group1, $group2) 167 | ); // integer grouping ID 168 | 169 | ``` 170 | 171 | **MailChimp API [create campaign](http://apidocs.mailchimp.com/api/1.3/campaigncreate.func.php) in a controller** 172 | 173 | ``` php 174 | get('MailChimp'); 176 | $campaign = $mailChimp->getCampaign(); 177 | $campaign->setType($type); 178 | $campaign->setSubject($subject); 179 | $campaign->setFromEmail($fromEmail); 180 | $campaign->setFromName($fromName); 181 | $campaign->setHTML($html); 182 | 183 | 184 | $campaign->create(); //return campaign id 185 | ``` 186 | 187 | **MailChimp API [send test campaign](http://apidocs.mailchimp.com/api/1.3/campaignsendtest.func.php) in a controller** 188 | 189 | ``` php 190 | get('MailChimp'); 194 | $campaign = $mailChimp->getCampaign(); 195 | $campaign->SendTest($campaignId, $emails); // return boolean 196 | 197 | ``` 198 | 199 | **MailChimp API [send campaign](http://apidocs.mailchimp.com/api/1.3/campaignsendnow.func.php) in a controller** 200 | 201 | ``` php 202 | get('MailChimp'); 205 | $campaign = $mailChimp->getCampaign(); 206 | $campaign->SendNow($campaignId); // return boolean 207 | ``` 208 | 209 | **MailChimp Export API [List](http://apidocs.mailchimp.com/export/1.0/list.func.php) in controller** 210 | 211 | ``` php 212 | get('MailChimp'); 214 | $export = $mailChimp->getExport(); 215 | $options = array('status' => 'unsubscribed'); //subscribed, unsubscribed, cleaned 216 | $export->DumpList($options); //return array 217 | 218 | ``` 219 | 220 | **MailChimp API [Listmemberinfo](http://apidocs.mailchimp.com/api/rtfm/listmemberinfo.func.php) in controller** 221 | ``` php 222 | get('MailChimp'); 224 | $list = $mailChimp->getList(); 225 | $list->getMemberInfo($email) 226 | ``` 227 | 228 | **MailChimp API [Import Ecommerce Order](http://apidocs.mailchimp.com/api/1.3/ecommorderadd.func.php) in controller** 229 | ``` php 230 | get('MailChimp'); 232 | $ecommerce = $mailChimp->getEcommerce(); 233 | 234 | $ecommerce->setOrderId($orderId) 235 | $ecommerce->setOrderDate($orderDate) 236 | $ecommerce->setStoreId($storeId) 237 | $ecommerce->setStoreName($storeName) 238 | $ecommerce->setCampaignId($mailChimpCampaigId) 239 | $ecommerce->setShipping($shippingTotal) 240 | $ecommerce->setTax($taxTotal) 241 | $ecommerce->setTotal($orderTotal) 242 | $ecommerce->addItem($productId, $productName, $categoryId, $categoryName, $qty, $cost, $sku) 243 | 244 | $ecommerce->addOrder($email) //return boolean 245 | ``` 246 | 247 | **MailChimp API [Delete Ecommerce Order](http://apidocs.mailchimp.com/api/1.3/ecommorderdel.func.php) in controller** 248 | ``` php 249 | get('MailChimp'); 251 | $ecommerce = $mailChimp->getEcommerce(); 252 | 253 | $ecommerce->deleteOrder($storeId, $orderId) //return boolean 254 | ``` 255 | 256 | **MailChimp API [Retrieve Ecommerce Orders](http://apidocs.mailchimp.com/api/1.3/ecommorders.func.php) in controller** 257 | ``` php 258 | get('MailChimp'); 260 | $ecommerce = $mailChimp->getEcommerce(); 261 | 262 | $ecommerce->getOrder($pageStart, $batchLimit, $dateSince) //return array 263 | ``` 264 | 265 | **MailChimp API [create static segment](http://apidocs.mailchimp.com/api/2.0/lists/static-segment-add.php) in a controller** 266 | 267 | ``` php 268 | get('MailChimp'); 271 | $list = $mailChimp->getList(); 272 | $list->listStaticSegmentAdd('first_segment'); // return int segment id 273 | 274 | ``` 275 | 276 | **MailChimp API [segment member add](http://apidocs.mailchimp.com/api/2.0/lists/static-segment-members-add.php) in a controller** 277 | 278 | ``` php 279 | get('MailChimp'); 282 | $list = $mailChimp->getList(); 283 | $segmentId = $list->listStaticSegmentAdd('first_segment'); 284 | $batch = array('test1@example.com', 'test2@example.com'); 285 | $list->listStaticSegmentMembersAdd($segmentId, $batch); 286 | 287 | ``` 288 | 289 | **MailChimp API [list static segment](http://apidocs.mailchimp.com/api/2.0/lists/static-segments.php) in a controller** 290 | 291 | ``` php 292 | get('MailChimp'); 295 | $list = $mailChimp->getList(); 296 | $segments = $list->listStaticSegments(); 297 | 298 | ``` 299 | 300 | **MailChimp API [send campaign to segment] in a controller** 301 | 302 | ``` php 303 | get('MailChimp'); 306 | $campaign = $mailChimp->getCampaign(); 307 | $list = $mailChimp->getList(); 308 | $segmentId = $list->listStaticSegmentAdd('first_segment'); 309 | $batch = array('test1@example.com', 'test2@example.com'); 310 | $list->listStaticSegmentMembersAdd($segmentId, $batch); 311 | $conditions[] = array( 312 | 'field' => 'static_segment', 313 | 'op' => 'eq', 314 | 'value' => $segmentId 315 | ); 316 | 317 | $segment_options = array( 318 | 'match' => 'all', 319 | 'conditions' => $conditions 320 | ); 321 | $campaign->setSegmenOptions($segment_options); 322 | $campaignId = $campaign->create(); 323 | $campaign->SendNow($campaignId); 324 | 325 | ``` 326 | 327 | **MailChimp API [campaigns](http://apidocs.mailchimp.com/api/rtfm/campaigns.func.php) in a controller** 328 | 329 | ``` php 330 | get('MailChimp'); 333 | $campaign = $mailChimp->getCampaign(); 334 | //get all campaigns 335 | $campaign->campaigns(); 336 | 337 | ``` 338 | ``` php 339 | get('MailChimp'); 342 | $campaign = $mailChimp->getCampaign(); 343 | //filters for example campaign_id, you can all the filters in api website 344 | $campaign->setFilters(array('campaign_id' => 4589)); 345 | $campaign->campaigns(); 346 | 347 | ``` 348 | 349 | **MailChimp API [campaignStats](http://apidocs.mailchimp.com/api/rtfm/campaignstats.func.php) in a controller** 350 | 351 | ``` php 352 | get('MailChimp'); 355 | $campaign = $mailChimp->getCampaign(); 356 | $campaign->campaignStats($campaignId); //return array(), struct of the statistics for this campaign 357 | 358 | 359 | ``` 360 | 361 | -------------------------------------------------------------------------------- /Services/Methods/MCCampaign.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace MZ\MailChimpBundle\Services\Methods; 13 | 14 | use MZ\MailChimpBundle\Services\HttpClient; 15 | 16 | /** 17 | * Mailchimp Campagin method 18 | * 19 | * @author Miguel Perez 20 | * @link http://apidocs.mailchimp.com/api/1.3/#campaignrelated 21 | */ 22 | class MCCampaign extends HttpClient 23 | { 24 | 25 | private $type; 26 | private $subject; 27 | private $fromEmail; 28 | private $fromName; 29 | private $html; 30 | private $text; 31 | private $toName = null; 32 | private $templateId = null; 33 | private $galleryTemplateId = null; 34 | private $baseTemplateId = null; 35 | private $folderId = null; 36 | private $tracking = null; 37 | private $title = null; 38 | private $authenticate = false; 39 | private $analytics = null; 40 | private $autoFooter = false; 41 | private $inlineCss = false; 42 | private $generateText = false; 43 | private $autoTweet = false; 44 | private $autoFBPost = null; 45 | private $fbComments = true; 46 | private $timeWarp = false; 47 | private $ecomm360 = false; 48 | private $segmentOptions = array(); 49 | private $filters = array(); 50 | 51 | /** 52 | * The list identificator 53 | * 54 | * @param integer $listId the list to which the message should be sent 55 | */ 56 | public function setListId($listId) 57 | { 58 | $this->listId = $listId; 59 | } 60 | 61 | /** 62 | * The Campaign Type 63 | * 64 | * @param string $type the campaign type to create 65 | */ 66 | public function setType($type) 67 | { 68 | $this->type = $type; 69 | } 70 | 71 | /** 72 | * The subject line for your campaign 73 | * 74 | * @param string $subject the subject line for your campaign message 75 | */ 76 | public function setSubject($subject) 77 | { 78 | $this->subject = $subject; 79 | } 80 | 81 | /** 82 | * The From email address for your campaign 83 | * 84 | * @param string $fromEmail email address for your campaign message 85 | */ 86 | Public function setFromEmail($fromEmail) 87 | { 88 | $this->fromEmail = $fromEmail; 89 | } 90 | 91 | /** 92 | * The From name for your campaign 93 | * 94 | * @param string $fromName name for your campaign message 95 | */ 96 | public function setFromName($fromName) 97 | { 98 | $this->fromName = $fromName; 99 | } 100 | 101 | /** 102 | * Campaign html 103 | * 104 | * @param string $html HTML content 105 | */ 106 | public function setHTML($html) 107 | { 108 | $this->html = $html; 109 | } 110 | 111 | /** 112 | * Campaign text 113 | * 114 | * @param string $text text content 115 | */ 116 | public function setText($text) 117 | { 118 | $this->text = $text; 119 | } 120 | 121 | /** 122 | * The to name for your campaign 123 | * 124 | * @param string $toName name recipients 125 | */ 126 | Public function setToName($toName) 127 | { 128 | $this->toName = $toName; 129 | } 130 | 131 | /** 132 | * User created template id 133 | * 134 | * @param integer $templateId user-created template id to generate the HTML content 135 | */ 136 | public function setTemplateId($templateId) 137 | { 138 | $this->templateId = $templateId; 139 | } 140 | 141 | /** 142 | * Gallery template id 143 | * 144 | * @param integer $galleryTemplateId template id from the public gallery to generate the HTML content 145 | */ 146 | public function setGalleryTemplateId($galleryTemplateId) 147 | { 148 | $this->galleryTemplateId = $galleryTemplateId; 149 | } 150 | 151 | /** 152 | * Base template id 153 | * 154 | * @param integer $baseTemplateId start-from-scratch template id 155 | */ 156 | public function setBaseTemplateId($baseTemplateId) 157 | { 158 | $this->baseTemplateId = $baseTemplateId; 159 | } 160 | 161 | /** 162 | * Folder id 163 | * 164 | * @param integer $folderId folder id 165 | */ 166 | public function setFolderId($folderId) 167 | { 168 | $this->folderId = $folderId; 169 | } 170 | 171 | /** 172 | * Which recipient actions will be tracked 173 | * 174 | * @param boolean $opens track campaign open 175 | * @param boolean $htmlClicks track html clicks in campaign 176 | * @param boolean $textClicks track text in campaign 177 | */ 178 | public function setTracking($opens = true, $htmlClicks = false, $textClicks = false) 179 | { 180 | $this->tracking['opens'] = $opens; 181 | $this->tracking['html_clicks'] = $htmlClicks; 182 | $this->tracking['text_clicks'] = $textClicks; 183 | } 184 | 185 | /** 186 | * Internal name to use for this campaign 187 | * 188 | * @param string $title internal compaign name 189 | */ 190 | public function SetTitle($title) 191 | { 192 | $this->title = $title; 193 | } 194 | 195 | /** 196 | * Enable authenticate 197 | */ 198 | public function setAuthenticate() 199 | { 200 | $this->authenticate = true; 201 | } 202 | 203 | /** 204 | * Set analytics 205 | * 206 | * @param array $analytics add analutics 207 | */ 208 | public function setAnalytics($analytics) 209 | { 210 | $this->analytics = $analytics; 211 | } 212 | 213 | /** 214 | * Enable auto footer 215 | */ 216 | public function SetAutoFooter() 217 | { 218 | $this->autoFooter = true; 219 | } 220 | 221 | /** 222 | * Enable inlinecss 223 | * 224 | * @param boolean $inlineCss allow inline css 225 | */ 226 | public function setInlineCss($inlineCss) 227 | { 228 | $this->inlineCss = $inlineCss; 229 | } 230 | 231 | /** 232 | * Enable genarate text 233 | * 234 | * @param boolean $generateText create text version of compaign 235 | */ 236 | public function setGenerateText($generateText) 237 | { 238 | $this->generateText = $generateText; 239 | } 240 | 241 | /** 242 | * Enable auto tweet 243 | * 244 | * @param boolean $autoTweet auto tweet compaign 245 | */ 246 | public function setAutoTweet($autoTweet) 247 | { 248 | $this->autoTweet = $autoTweet; 249 | } 250 | 251 | /** 252 | * Auto post to facebook page 253 | * 254 | * @param array $autoFBPost array with facebook pages id 255 | */ 256 | public function setAutoFBPost($autoFBPost) 257 | { 258 | $this->autoFBPost = $autoFBPost; 259 | } 260 | 261 | /** 262 | * Enable Facebook comments 263 | * 264 | * @param boolean $fbComments add facebook comments 265 | */ 266 | public function setFbComments($fbComments) 267 | { 268 | $this->fbComments = $fbComments; 269 | } 270 | 271 | /** 272 | * Enable time warp 273 | * 274 | * @param boolean $timeWarp campaign must be scheduled 24 hours in advance of sending 275 | */ 276 | public function setTimeWarp($timeWarp) 277 | { 278 | $this->timeWarp = $timeWarp; 279 | } 280 | 281 | /** 282 | * Enable Ecommerce360 tracking 283 | * 284 | * @param boolean $ecomm360 Ecommerce360 tracking will be enabled 285 | */ 286 | public function setEcomm360($ecomm360) 287 | { 288 | $this->ecomm360 = $ecomm360; 289 | } 290 | 291 | /** 292 | * Set mailchimp segmentOptions 293 | * 294 | * @param array segmentOptions 295 | */ 296 | public function setSegmenOptions(Array $segment_options) 297 | { 298 | $this->segmentOptions = $segment_options; 299 | } 300 | 301 | /** 302 | * Set mailchimp filters 303 | * 304 | * @param array filters 305 | */ 306 | public function setFilters(Array $filters) 307 | { 308 | $this->filters = $filters; 309 | } 310 | 311 | /** 312 | * Create options 313 | * 314 | * @return array 315 | */ 316 | private function Options() 317 | { 318 | $option = array('subject' => $this->subject, 319 | 'list_id' => $this->listId, 'from_email' => $this->fromEmail, 320 | 'from_name' => $this->fromName, 321 | 'template_id' => $this->templateId, 322 | 'gallery_template_id' => $this->galleryTemplateId, 323 | 'base_template_id' => $this->baseTemplateId, 324 | 'folder_id' => $this->folderId, 'tracking' => $this->tracking, 325 | 'title' => $this->title, 'authenticate' => $this->authenticate, 326 | 'analytics' => $this->analytics, 327 | 'auto_footer' => $this->autoFooter, 328 | 'inline_css' => $this->inlineCss, 329 | 'generate_text' => $this->generateText, 330 | 'auto_tweet' => $this->autoTweet, 331 | 'auto_fb_post' => $this->autoFBPost, 332 | 'fb_comments' => $this->fbComments, 333 | 'timewarp' => $this->timeWarp, 'ecomm360' => $this->ecomm360); 334 | 335 | return $option; 336 | } 337 | 338 | /** 339 | * Create content 340 | * 341 | * @return array 342 | */ 343 | private function Content() 344 | { 345 | $content = array('html' => $this->html, 'text' => $this->text); 346 | return $content; 347 | } 348 | 349 | /** 350 | * Create campaign 351 | * 352 | * @return integer 353 | */ 354 | public function Create() 355 | { 356 | if(empty($this->segmentOptions)){ 357 | $payload = array('type' => $this->type, 'options' => $this->Options(), 358 | 'content' => $this->Content()); 359 | }else { 360 | $payload = array('type' => $this->type, 'options' => $this->Options(), 361 | 'content' => $this->Content(), 'segment_opts' => $this->segmentOptions); 362 | } 363 | $apiCall = 'campaignCreate'; 364 | $data = $this->makeRequest($apiCall, $payload); 365 | $data = json_decode($data); 366 | 367 | return $data; 368 | } 369 | 370 | /** 371 | * Send test compaign 372 | * 373 | * @param string $campaignId campaign id 374 | * @param array $emails test emails 375 | * 376 | * @return boolean 377 | */ 378 | public function SendTest($campaignId, $emails) 379 | { 380 | $payload = array('cid' => $campaignId, 'test_emails' => $emails); 381 | 382 | $apiCall = 'campaignSendTest'; 383 | $data = $this->makeRequest($apiCall, $payload); 384 | $data = json_decode($data); 385 | 386 | return $data; 387 | } 388 | 389 | /** 390 | * Send compaign 391 | * 392 | * @param string $campaignId campaign id 393 | * 394 | * @return boolean 395 | */ 396 | public function SendNow($campaignId) 397 | { 398 | $payload = array('cid' => $campaignId); 399 | 400 | $apiCall = 'campaignSendNow'; 401 | $data = $this->makeRequest($apiCall, $payload); 402 | $data = json_decode($data); 403 | 404 | return $data; 405 | } 406 | 407 | /** 408 | * list of campaigns and their details 409 | * 410 | * @return array 411 | */ 412 | public function campaigns() 413 | { 414 | $payload = array('filters' => $this->filters); 415 | 416 | $apiCall = 'campaigns'; 417 | $data = $this->makeRequest($apiCall, $payload); 418 | $data = json_decode($data); 419 | 420 | return $data; 421 | } 422 | 423 | /** 424 | * get all the relevant campaign statistics (opens, bounces, clicks, etc.) 425 | * 426 | * @param string $campaignId campaign id 427 | * 428 | * @return array 429 | */ 430 | public function campaignStats($campaignId) 431 | { 432 | $payload = array('cid' => $campaignId); 433 | 434 | $apiCall = 'campaignStats'; 435 | $data = $this->makeRequest($apiCall, $payload); 436 | $data = json_decode($data); 437 | 438 | return $data; 439 | } 440 | } 441 | --------------------------------------------------------------------------------