MailChimp API Integration Tutorial in PHP

MailChimp is an email marketing platform which is very popular and widely accepted from small businesses to enterprises. Here in this tutorial, I am going to show you how to do MailChimp API integration in PHP. This tutorial will work on current MailChimp API Version 3.0.

So, let’s start.

Create the Form

HTML Form for MailChimp API Integration in PHP

At first, we will create the form, from which users can submit their details and it will go directly in the MailChimp list (or audience for newly created MailChimp accounts). So this is will be simple HTML form elements with bootstrap classes.

So the form is getting posted to  form-submit.php file. Now we will create the file in the same directory in which this HTML file will be.

Submit the Form in PHP

First, we are checking if the file is getting a post request. If it is true, then assign every input field value in respective PHP variables. And to see if our form and PHP is working as it should be I have made few echo statements.

MailChimp API V 3.0 Integration

In this stage, we will now actually work on integration. For this, I am going to use a MailChimp API library from GitHub. Now I created another directory called /lib/  within our project directory and within this directory, I have placed the MailChimp.php file, which I got from the above mentioned library.

Now in our  form-submit.php file, we need to include it and do other necessary coding.

In line 6 & 7, you need to provide your API key and List/Audience ID respectively. To see how you can find the API key & list ID see my tutorial video at the end of this tutorial.

In line 20, I have specified merge fields which are custom user data. To create custom data fields in MailChimp go to your list, then go to settings -> List fields & *|MERGE|* tags and create fields as per your requirement. For more details see the video tutorial mentioned below.

Custom Fields - MailChimp API Integration in PHP

Later in the code, we are checking MailChimp response and if we get success then we can print our custom success message to the user, otherwise the error message.

So, this is it. Hope this tutorial helps you. If you face any difficulties in the process let me know by commenting down below.

And before ending the tutorial here is my video representation of this tutorial.

Leave a Reply

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