Skip to content

A MailChimp API v3 wrapper for WordPress applications (based on Drew McLellan's MailChimp API)

License

Notifications You must be signed in to change notification settings

rocketgeek/mailchimp-api

Repository files navigation

Mailchimp API for WordPress

A simple Mailchimp API v3 wrapper in PHP based on Drew McLellan's project, rewritten to use WordPress' HTTP API and packaged for simple use in plugins and themes without the need for Composer.

Requires PHP 5.4 and WordPress

Installation

Add the library to your project and include the main file.

include( 'your/actual/path/class-rocketgeek-mailchimp-api.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( 'your/actual/path/class-rocketgeek-mailchimp-api-batch.php' ); 
include( 'your/actual/path/class-rocketgeek-mailchimp-api-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 adding the class and creating an instance with your API key

$my_wp_chimp = new RocketGeek_Mailchimp_API( $my_api_key );

Note: If you're using this in a plugin or theme project, your API key can (and probably should) be coming from an option in WP. Also, you can optionally specify an $api_endpoint as a second argument if you need a second instance or you need your endpoint to be something other than 'https://.api.mailchimp.com/3.0'.

You can list all the mailing lists (with a get on the lists method)

$result = $my_wp_chimp->get( 'lists' );

print_r( $result );

Subscribe someone to a list (with a post to the lists/{listID}/members method):

$list_id = 'b1234346';

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

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

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

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

$list_id = 'b1234346';

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

if ( $my_wp_chimp->success() ) {
	print_r( $result );	
} else {
	echo $my_wp_chimp->get_last_error();
}

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.

$my_wp_chimp       = new RocketGeek_Mailchimp_API( $my_api_key );
$my_wp_chimp_batch = $my_wp_chimp->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.

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

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

$my_wp_chimp_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 = $my_wp_chimp_batch->execute();

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

$my_wp_chimp->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, but if you're still out there running this then you've got serious security issues.

Mailchimp webhooks enable your code to be notified of changes to lists and campaigns. If you want to build a "two-way" application that receives information from Mailchimp as well as sending, this is what you need.

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:

RocketGeek_Mailchimp_API_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:

$result = RocketGeek_Mailchimp_API_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 $my_wp_chimp->getLastError();

For further debugging, you can inspect the headers and body of the response:

print_r($my_wp_chimp->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($my_wp_chimp->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.

Versioning

I use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

This project is licensed under the Apache-2.0 License - see the LICENSE file for details.

I hope you find this project useful. If you use it your project, attribution is appreciated.

About

A MailChimp API v3 wrapper for WordPress applications (based on Drew McLellan's MailChimp API)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages