Super-simple, minimum abstraction MailChimp API v3 wrapper, in PHP.
I hate complex wrappers. This lets you get from the MailChimp API docs to the code as directly as possible.
Requires PHP 5.3 and a pulse. Abstraction is for chimps.
You can install mailchimp-api using Composer:
composer require drewm/mailchimp-api
You will then need to:
- run
composer install
to get these dependencies added to your vendor directory - add the autoloader to your application with this line:
require("vendor/autoload.php")
Alternatively you can just download the MailChimp.php file and include it manually:
include('./Mailchimp.php');
Start by use
-ing the class and creating an instance with your API key
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1')
Then, list all the mailing lists (with a get
on the lists
method)
$result = $MailChimp->get('lists');
print_r($result);
Subscribe someone to a list (with a post
to the list/{listID}/members
method):
$list_id = 'b1234346';
$result = $MailChimp->post("lists/$list_id/members", [
'email_address' => 'davy@example.com',
'status' => 'subscribed',
]);
print_r($result);
Update a list member with more information (using patch
to update):
$list_id = 'b1234346';
$subscriber_hash = $MailChimp->subscriberHash('davy@example.com');
$result = $MailChimp->patch("lists/$list_id/members/$subscriber_hash", [
'merge_fields' => ['FNAME'=>'Davy', 'LNAME'=>'Jones'],
'interests' => ['2s3a384h' => true],
]);
print_r($result);
Remove a list member using the delete
method:
$list_id = 'b1234346';
$subscriber_hash = $MailChimp->subscriberHash('davy@example.com');
$MailChimp->delete("lists/$list_id/members/$subscriber_hash");
To get the last error returned by either the HTTP client or by the API, use getLastError()
:
echo $MailChimp->getLastError();
For further debugging, you can inspect the headers and body of the response:
print_r($MailChimp->getLastResponse());
If your server's CA root certificates are not up to date you may find that SSL verification fails and you don't get a response. The correction solution for this is not to disable SSL verification. The solution is to update your certificates. If you can't do that, there's an option at the top of the class file. Please don't just switch it off without at least attempting to update your certs -- that's lazy and dangerous. You're not a lazy, dangerous developer are you?
This is a fairly simple wrapper, but it has been made much better by contributions from those using it. If you'd like to suggest an improvement, please raise an issue to discuss it before making your pull request.
Pull requests for bugs are more than welcome - please explain the bug you're trying to fix in the message.
There are a small number of PHPUnit unit tests. To get up and running, copy .env.example
to .env
and add your API key details. Unit testing against an API is obviously a bit tricky, but I'd welcome any contributions to this. It would be great to have more test coverage.