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…

The perfect custom e-commerce website for your business may be best left as a dream for now.

Most small businesses don’t need a custom e-commerce website. When an off the shelf product will work.

If your small business is tight on funds as most small businesses are. You don’t want to tie up your cash flow with a custom e-commerce dream site.

Think of it another way. Say you need to buy a car. Will you buy a car that’s already made and ready to go? Or would you order a custom made car, to your exact specifications? Are you prepared to pay for the extra cost and wait for it to be made for you?

And that’s after the time it will take to find a developer or company to make the site for you.

Almost everyone will choose a ready made car. So choose a ready made, easy to configure e-commerce solution.

Time, money and patience is required for custom software development.

Custom software development can really hurt, so do your research before committing to it.

Developing a customer e-commerce website can be very time consuming. Do you really need to try and “re-invent the wheel”?

The development process can also be annoying and irritating. More so if you don’t understand the technical aspect. Even seemingly small visual changes can seem to take way more time than you think they should.

You might start thinking…

“Am I being ripped off by my website developer?”

If you haven’t yet embarked down this process, pause and consider your options:

Pay to develop custom software to exactly meet your needs. Which could be a long frustrating expensive journey or

Use an existing e-commerce service like Shopify. Where in a few hours you could have a professional looking site live and ready to take orders.

I’m a software developer. Through years of experience of being paid to develop custom software. I recommend you avoid all the potential pains associated with option 1. Take option 2, start accepting orders today!

Save yourself the pain, take the easy way of using software. That you can then customize (within certain boundaries) for yourself!

If you liked this post about developing a custom e-commerce website, be sure to subscribe below for my next post

What comes first your wordpress blog or your website?

You might have started with a free wordpress blog. Then later created a separate website or vice versa. Well whichever way round it is. You might have ended up with two disconnected websites. Now you want to reuse your wordpress.com blog content in your website.

One easy way to do this and integrate your blog content in your website is using its RSS feed. Which is available on your wordpress blog. Just add this query string “?feed=rss” to your blog’s url and you will get an RSS feed from your blog.

If you are just starting out with PHP, you may come across this message:

Notice: Undefinedindex: username in C:\wamp\www\mysite\index.php on line 99

Even if you’ve been coding for years you can still get this message!

You may be confused as to what an “Undefined index” is? Often seen when trying to access global server variables such as $_POST and $_GET when passing data from another page or posting it back to the same page.

So what is it? It really is an index of an array that hasn’t been defined yet. You are trying to access data in a variable that doesn’t exist!

Depending on the PHP error reporting level you configure, you can even hide these notices, but that’s not recommended, better to deal with them and write good code.

You can normally fix your “Undefined index” errors with the isset function to check whether a variable is set, like this:

However a very useful function which can save you many lines of code like the above and undefined index notices, can be found in the comments (all credit to the author) on the same page of the PHP manual.

//Example usage which will give the same result as the if / else statement above
$username = isset_or($_POST['username'], "");

It’s a very useful function if you are using plain PHP and need to determine the value of variables. It will also make your code cleaner and easier to read than repeated if / else statements like the above.

Life can be stressful enough, work as a web developer has it’s own unique stresses from dealing with users who don’t know what they are doing, managers who don’t know what they are managing and technology that never stands still.

At times and some days at multiple times in the day the stress can feel too much. Sometimes you feel like you need to release the built up angst, anger and frustrations. Venting and releasing the stress in a controlled manner is important for you and everyone around you.

There are times when you can spend hours trying to fix a bug, that can turn out to be a single character (often invisible) that’s out of place causing a bug and stress.

There are lots of options for dealing with stress, but one that is free and should be available to almost anyone, is just getting up and going for a walk. Preferably in natural surroundings, if you are in a city, try and walk around a nearby park.

Take a break, when you are stuck go for a walk. Disconnect from your problem, when you come back after your 10-20 minute walk, you will have a fresh perspective. I do this all the time and it is crucial to my productivity.

It’s amazing how bugs can be solved, by just leaving the code alone, getting outside for a walk for 10-20 minutes and then when you come back, you have the fix and the stress is gone! Often I find the fix will come to me halfway on the walk, or when I’m on my way back to the office. When you are desperately trying to fix an urgent problem, it’s amazing how time can fly by, so stop, get up and take a break with a walk and let the solution come to you, while the stress dissipates…

How to protect your PHP source code? You might be worried about giving away your secret sauce or having it reverse engineered.

Another good reason for wanting to protect your code is delivering the code to a clients server / hosting before final payment has been made for the development of the code.

I have experienced this first hand! I was very surprised to find a delivery of code for sign off was encrypted, I hadn’t seen that before. Sure enough when we asked the development company they said they would remove the encryption once the final payment had been made (there already had been a number of part payments through the project).

So definitely a useful tool for small shops and freelancers, make it a part of your contract that the source code will be decrypted when fully paid for.

We also had another client who wanted to distribute PHP code on USB thumb sticks. Not recommended, but it is possible to run a web server like that, using a SQLite database. Given the accessibility of a USB thumb stick rather than a Linux server, we encrypted the PHP code to protect it.