How to add a subscriber using MailChimps API v3.0 with PHP and CURL
A quick step-by-step guide with PHP code
Here was the problem, my greeting card website (PamitCards.ca) has a list of subscribers sitting in a MySQL database table. Whenever I wanted to use MailChimp to send the list of subscribers an email. I would have to export the email addresses as a CSV file. Then log-in to MailChimp, import the file so that any new subscribers would be added. MailChimp’s import process would then email me with a list of additions, bounced and un-subscribed email addresses. I would then take those and update the MySQL database table removing the redundant addresses.
Whoa that’s a lot of steps, quite a time consuming process for something that could be automated / integrated!
Realizing that using MailChimp’s API, I could add subscribers to my MailChimp list. Thereby making the above process redundant along with the database table. What’s the point of keeping that data if it’s in MailChimp.
So here’s how to make it work…
- If you are reading this, I’ll assume you already have a MailChimp account and at least one list set-up. If not it’s really easy to sign-up and get started with MailChimp.
- Then you will need an API key if you don’t have one already, find it under Account > Extras > API keys. Note that running the code locally under WAMP I had to change the https to http to make it work.
- Once your key is generated, you will use the last 3 characters as part of the API call, in my case it was us2 to make this url https://us2.api.mailchimp.com/3.0
- Then you will need to generate your Auth token, I used Postman which is available in the Chrome Web store to add to your browser.
- Using Basic Auth you can put in your Username and Password and this will generate a string that looks like this Basic cGFtaXRjYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZGUwNS11czI=
- First do a GET call to http://us2.api.mailchimp.com/3.0/lists this will return a list of your lists, find your relevant list id to insert in the code below along with your API key
- Then specify an email address to Subscribe and run the code.
- You should get a response in the browser showing the the email address has been added
- You can extra fields if your list and signup process has them.
- Any problems, please let me know with a comment or follow me on Twitter for more helpful posts…
Here’s the PHP code I used: