This article will walk you through the process of implementing MailChimp into your instance of DotKernel API using drewm/mailchimp-api

 

Step 1: Add the library to your application using the following command:
composer require drewm/mailchimp-api

 

Step 2: Create configuration file config/autoload/mailchimp.global.php and paste the following content inside of it:

<?php

declare(strict_types=1);

return [
    'mailChimp' => [
        'apiKey' => '{YOUR_API_KEY_HERE}'
        // you can add more MailChimp-related parameters to this array
    ]
];

 

Step 3: Create factory src/App/src/MailChimp/Factory/MailChimpFactory.php which will return an instance of DrewM\MailChimp. Paste the following content inside this file:

<?php

declare(strict_types=1);

namespace Api\App\MailChimp\Factory;

use DrewM\MailChimp\MailChimp;
use Psr\Container\ContainerInterface;

/**
 * Class MailChimpFactory
 * @package Api\App\MailChimp\Factory
 */
class MailChimpFactory
{
    /**
     * @param ContainerInterface $container
     * @return MailChimp
     * @throws \Exception
     */
    public function __invoke(ContainerInterface $container) : MailChimp
    {
        $config = $container->get('config')['mailChimp'] ?? [];

        return new MailChimp($config['apiKey'] ?? '');
    }
}

 

Step 4: Let your application use this factory by adding it to the main ConfigProvider:
To do this, open file src/App/src/ConfigProvider.php and locate the method called getDependencies().
Inside this method, locate the key factories which points to an array. Inside this array add the following line:

MailChimp::class => MailChimpFactory::class,

Make sure you you add the corresponding uses:

use Api\App\MailChimp\Factory\MailChimpFactory;
use DrewM\MailChimp\MailChimp;

 

After this, you can start using the library by @Injecting MailChimp::class where it’s needed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>