All Projects → drewm → Mailchimp Api

drewm / Mailchimp Api

Licence: mit
Super-simple, minimum abstraction MailChimp API v3 wrapper, in PHP

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to Mailchimp Api

mailchimp-marketing-php
The official PHP client library for the Mailchimp Marketing API
Stars: ✭ 88 (-95.55%)
Mutual labels:  mailchimp, mailchimp-api, mailchimp-api-wrapper, mailchimp-php
mailchimp-marketing-python
The official Python client library for the Mailchimp Marketing API
Stars: ✭ 66 (-96.66%)
Mutual labels:  mailchimp, mailchimp-api, mailchimp-api-wrapper
mailchimp-marketing-node
The official Node.js client library for the Mailchimp Marketing API
Stars: ✭ 106 (-94.64%)
Mutual labels:  mailchimp, mailchimp-api, mailchimp-api-wrapper
mailchimp-marketing-ruby
The official Ruby client library for the Mailchimp Marketing API
Stars: ✭ 25 (-98.74%)
Mutual labels:  mailchimp, mailchimp-api, mailchimp-api-wrapper
cf-mailchimp
ColdFusion wrapper for the MailChimp 3.0 API
Stars: ✭ 17 (-99.14%)
Mutual labels:  mailchimp, mailchimp-api
mailchimp
A basic Elixir wrapper for version 3 of the MailChimp API
Stars: ✭ 42 (-97.88%)
Mutual labels:  mailchimp, mailchimp-api
mailchimp-bundle
MailChimp integration with Symfony and MailChimp API V3
Stars: ✭ 40 (-97.98%)
Mutual labels:  mailchimp, mailchimp-api
Mailjet Apiv3 Nodejs
[API v3] Official Mailjet API v3 NodeJS wrapper
Stars: ✭ 137 (-93.07%)
Mutual labels:  wrapper
Trado
Trado is a lightweight, easy to use ecommerce platform; designed to allow developers to quickly deploy a premium ecommerce store for their business
Stars: ✭ 149 (-92.46%)
Mutual labels:  mailchimp
Angular Diff Match Patch
An AngularJS wrapper for google-diff-match-patch
Stars: ✭ 135 (-93.17%)
Mutual labels:  wrapper
Libfaketime
libfaketime modifies the system time for a single application
Stars: ✭ 1,932 (-2.28%)
Mutual labels:  wrapper
Asana Api Php Class
A dependency free, lightweight PHP class that acts as wrapper for Asana API. Lets make things fast and easy! :)
Stars: ✭ 137 (-93.07%)
Mutual labels:  wrapper
Vue Axios
A small wrapper for integrating axios to Vuejs
Stars: ✭ 1,887 (-4.55%)
Mutual labels:  wrapper
Python Twitch Client
Python wrapper for Twitch API
Stars: ✭ 137 (-93.07%)
Mutual labels:  wrapper
Desktop Google Keep Osx
A Super Simple Desktop Client for Mac OSX Built in Javascript and MacGap
Stars: ✭ 159 (-91.96%)
Mutual labels:  wrapper
Mastodonkit
MastodonKit is a Swift Framework that wraps Mastodon's API
Stars: ✭ 134 (-93.22%)
Mutual labels:  wrapper
T14m4t
Automated brute-forcing attack tool.
Stars: ✭ 160 (-91.91%)
Mutual labels:  wrapper
Py Kaldi Asr
Some simple wrappers around kaldi-asr intended to make using kaldi's (online) decoders as convenient as possible.
Stars: ✭ 156 (-92.11%)
Mutual labels:  wrapper
Coinbasepro Csharp
The unofficial .NET/C# client library for the Coinbase Pro/GDAX API
Stars: ✭ 143 (-92.77%)
Mutual labels:  wrapper
Heed
A fully typed LMDB/MDBX wrapper with minimum overhead
Stars: ✭ 142 (-92.82%)
Mutual labels:  wrapper

MailChimp API

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.

Build Status Scrutinizer Code Quality Packagist

Installation

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'); 

If you wish to use the batch request or webhook interfaces, you'll also need to download and include the Batch.php or Webhook.php files:

include('./Batch.php'); 
include('./Webhook.php'); 

These are optional. If you're not using batches or webhooks you can just skip them. You can always come back and add them later.

Examples

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 lists/{listID}/members method):

$list_id = 'b1234346';

$result = $MailChimp->post("lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

print_r($result);

Update a list member with more information (using patch to update):

$list_id = 'b1234346';
$subscriber_hash = MailChimp::subscriberHash('[email protected]');

$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('[email protected]');

$MailChimp->delete("lists/$list_id/members/$subscriber_hash");

Quickly test for a successful action with the success() method:

$list_id = 'b1234346';

$result = $MailChimp->post("lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

if ($MailChimp->success()) {
	print_r($result);	
} else {
	echo $MailChimp->getLastError();
}

Batch Operations

The MailChimp Batch Operations functionality enables you to complete multiple operations with a single call. A good example is adding thousands of members to a list - you can perform this in one request rather than thousands.

use \DrewM\MailChimp\MailChimp;
use \DrewM\MailChimp\Batch;

$MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1');
$Batch 	   = $MailChimp->new_batch();

You can then make requests on the Batch object just as you would normally with the MailChimp object. The difference is that you need to set an ID for the operation as the first argument, and also that you won't get a response. The ID is used for finding the result of this request in the combined response from the batch operation.

$Batch->post("op1", "lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

$Batch->post("op2", "lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

$Batch->post("op3", "lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

Once you've finished all the requests that should be in the batch, you need to execute it.

$result = $Batch->execute();

The result includes a batch ID. At a later point, you can check the status of your batch:

$MailChimp->new_batch($batch_id);
$result = $Batch->check_status();

When your batch is finished, you can download the results from the URL given in the response. In the JSON, the result of each operation will be keyed by the ID you used as the first argument for the request.

Webhooks

Note: Use of the Webhooks functionality requires at least PHP 5.4.

MailChimp webhooks enable your code to be notified of changes to lists and campaigns.

When you set up a webhook you specify a URL on your server for the data to be sent to. This wrapper's Webhook class helps you catch that incoming webhook in a tidy way. It uses a subscription model, with your code subscribing to whichever webhook events it wants to listen for. You provide a callback function that the webhook data is passed to.

To listen for the unsubscribe webhook:

use \DrewM\MailChimp\Webhook;

Webhook::subscribe('unsubscribe', function($data){
	print_r($data);
});

At first glance the subscribe/unsubscribe looks confusing - your code is subscribing to the MailChimp unsubscribe webhook event. The callback function is passed as single argument - an associative array containing the webhook data.

If you'd rather just catch all webhooks and deal with them yourself, you can use:

use \DrewM\MailChimp\Webhook;

$result = Webhook::receive();
print_r($result);

There doesn't appear to be any documentation for the content of the webhook data. It's helpful to use something like ngrok for tunneling the webhooks to your development machine - you can then use its web interface to inspect what's been sent and to replay incoming webhooks while you debug your code.

Troubleshooting

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 you suspect you're sending data in the wrong format, you can look at what was sent to MailChimp by the wrapper:

print_r($MailChimp->getLastRequest());

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?

If you have high-level implementation questions about your project ("How do I add this to WordPress", "I've got a form that takes an email address...") please take them to somewhere like StackOverflow. If you think you've found a bug, or would like to discuss a change or improvement, feel free to raise an issue and we'll figure it out between us.

Contributing

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. 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].