Good Morning All,

I've worn myself out looking for the solution to integrating MailChimp APIs into my PHP scripts.

Is there anyone in here that is familiar with doing that?

I have the simple part done, where people can subscribe to a Newsletter to get more info, in which case they fall into the category of 'visitor' in the mail list, but the next 2 steps I don't understand how to implement.

This is the docs for the process: https://apidocs.mailchimp.com/api/how-to/sync-you-to-mailchimp.php

But when it gets to Figure out how you want to connect to the API, I am totally lost.

What I need to do is to use an API call to MailChimp in my php script so when someone registers as a 'member' of my site through the form in my back office. That API call would either add them to my maillist as a 'member' or if the email already exists as a 'visitor', it would upgrade that record to 'member'.

Then the second one is almost identical, but set up so when they make a purchase, it would do the API call to update their record from 'member' to 'customer' since they would have to be a 'member' before they ever make a purchase to become a 'customer'.

Any Help or direction would be Greatly appreciated.

I've been trying to figure this out on and off for about 3 weeks now.

Thanks in advance.

Douglas

Recommended Answers

All 9 Replies

Hi Douglas,

Do you have any examples of what you've tried to date? I could write something in PHP that talks to the API, but without your input, it won't truly be helpful. Maybe some of the code area where you are looking to add the API call for an example.

I haven't 'tried' anything because I can't get a handle on what it is that I'm supposed to do to make it happen.

I'm assuming I would use an 'API Wrapper' because they say that is the easiest, but still apparently not easy enough for me.

They have downloads here https://apidocs.mailchimp.com/api/downloads/

But I come to a screeching halt when I scroll down to PHP section and select either V1 or V2... it gets into things that I have no knowledge of, including Shell/Terminal/Command Line

I know I sound helpless, but I'm not really, just lost and trying to be found.

I think the logical place for the API call would be after they have filled out the registration form, and I do the error checking on their entries, and when there are no errors, I then insert their member record. At that point I'm envisioning doing the API call to push the 5 fields of into to the maillist.

Once I understand how to make the connection and the structure of the API call, I can most likely manage it, but seem to be at an educational impass at the moment.

This requires some sort of database to keep current user data, such as MySQL or PostgreSQL. You need to write your php scripts to capture the data to the database, and then the script to sync the data to mailchimp has to read that data. This is not a trivial task. How much PHP experience do you have?

I have been writing PHP / MySql for quite some time.

I have the entire website completed and ready for launch, but before I launch I want to integrate the MailChimp into the registration process.

Everything is already being stored in the member table in the DB, I just need to implement the API Calls necessary to push that data to the MailChimp email list.

I will assume you've followed some of these steps already - I am mostly listing them to insure everyone is on the same page.

Pre-Requisites
  • Web Server (phpserver of some sort)
  • Mail chimp oath secret and connnect (DO NOT DISCLOSE)
  • Database (you've confirmed you have one, and assuming mysql.)
Setup
  1. Find the appropriate API wrapper
  2. Initializing the wrapper
  3. Transferring data
Find the appropriate API wrapper

There are many API wrappers, and it would also be possible to write a direct API interface yourself. These are all located here on the MailChimp download page. The difference between the provided API's and writing your own is how the interaction occurs. If you write your own, you are writing direct restful'esque url requests to mailchimp. If you use a pre-built API, you require a file and then call into the required file (the mailchimp API docs might not be extremely useful at this point).

For the officail client, I would expect the documentation you referenced to be more useful, additionally - examples of usage are located here: examples.

Initializing the wrapper

Once you have the appropriate API Client downloaded and in place, you can 'require' it into your php code, initialize the class - then start calling onto the standard API methods.

  • require the client source php
  • register client with you secret key
  • Send a request through pre-built client.
  • Handle the response.
Transferring data

It sounds like you've marginally completed this portion - but possibly only using the simple API. I would need to know more about your specific implementation to provide further assistance here.

Moving forward

If you've made it through most of this already, it should be possible to provide a more specific 'sticking' point. Nobody needs to see your secret key / email list in order to assist you further. As there are a lot of moving parts, knowing which client, and how you are attempting to call it is extemely important for further troubleshooting.

Thanks for that post kwiering. And yes, I've been to those pages numerous, but always seem to bump up against things that I don't understand or am not familiar with as far as how to make the next step.

I think my understanding is clarifying a little bit. I downloaded the MCAPI v1.3.2 (current) and started reading the readme file and studying the class and the available functions.

I'm not sure if I'm on the right path, but it appears that I could use my email verification script so when someone verifies their email either after registering (as a member) or by changing their email address from their back office (as a member or customer).

In that script, I would include the class, and then at the correct place in the script, once the new email address has been verified, do a call to the list subscribe function or the list update function based on what is being done.

It seems to me that if it is as easy as that, then I should have the resolution to my questions... I will let you know if that actually will work that easily.

Thanks for sticking with me and giving me what is apparently the confidence necessary to at least attempt to take the next step on my own.

Hopefully that is all it takes.

Douglas

Hey Douglas,
I was wondering if this request is at a point of resolution. I understand there may be more (specific) questions.

It is always possible to reference this thread in future questions related to this topic as well.

Thank-you,

Kyle

Kyle,

Yes, I think I have the resolution and as soon as I am 100%, I will post it here to share with anyone interested...

I was told by a couple programmers that it couldn't be done, but I'm of the opinion that anything can be done if you are persistent enough.

Thanks for checking on it.

Douglas

OK, I have Mailchimp working perfectly. Exactly the way I like things.

1st, there is the basic form that you can provide to visitors to register for more information or for a newsletter, or whatever.

I set up my mail list with the normal information - first / last / email
then I added a category / memberID / username as well

The category field I use to determine what type of subscriber it is..
if they subscribe for more info or newsletter, I record them as a 'Visitor' and they don't have any memberID or username.

When someone joins/registers as a member using my normal registration form on the site, I make sure that all of the variables have values assigned that would be required in the mail list, including memberID and Username used to register.

When someone registers they must Verify their email address to gain access to their back office, and that just seemed like the logical place to update their information on the mail list, since I know at that point that their email entered is verifiable.

So I simply do an include of a short little script that calls the related function in the MCAPI Class. I would include the class in here but it is 3000 lines long, so probably not a good idea.

// use this include in the verify script
include "mclinks/mcapi_listSubscribe.php" ; // New Subscription 

// to call this listSubscribe script

/**
Subscribe a New Member to a List using the MCAPI.php
class and do some basic error checking.
**/
require_once 'mclinks/MCAPI.class.php';
require_once 'mclinks/config.inc.php'; //contains apikey

$api = new MCAPI($apikey);

$merge_vars = array('EMAIL'=>$mem_cur_email, 'FNAME'=>$mem_fname, 'LNAME'=>$mem_lname,
                     'CATEGORY'=>$mem_category, 'USER'=>$mem_user, 'MEM_ID'=>$mem_id
                    );

// disabled sending a confirmation email - by setting double_optin=false
$retval = $api->listSubscribe( $listId, $my_email, $merge_vars, $email_type='html', $double_optin=false, $update_existing=true );

if ($api->errorCode){
  $Code = $api->errorCode;
  $Msg = $api->errorMessage;

  tech_alert('<br>Unable to load listSubscribe()!<br>Code='.$Code.'<br>Msg='.$Msg.'<br>my mail value:'.$my_email);

} else {

  $Code = '';
  $Msg = '';

  tech_alert('<br>Subscribed - <br>Returned: '.$retval);

}

the config file that is required is what contains all the private information relevant to my account and list.

the tech_alert is just a little function I use to email myself when something happens that shouldn't, or to tell me something works during testing.

As it turns out, my script that allows members to update their personal information, which sometimes includes their email address, also forces them to verify if they changed it, so I wrote some conditional code to determine if it was an add or an update, and a seperate little script to call the listUpdate function from the class.

But in talking with the support staff at MailChimp (very knowledgable and helpful, once you get to them), they suggested that I forego the update script.

They suggested just using the listSubscribe for both of them because the way it works is if you are subscribing with an existing email address, and have the flag set to update if exists, it works the same way as the update and just updates the fields that are different.

So now, if someone registers that had subscribed as a visitor, the subscribe script automatically changes their category from visitor to member and adds their memberID and username to the mail list record for future use

and if they hadn't subscribed as a visitor, it simply adds a new record.

And when a member makes their first purchase, I call the same script which basically just changes their category from 'member' to 'customer'

Now, I can send emails to each category individually or any combination of the 3, depending on what the email is intended for.

it was as simple as adding 1 folder in my public html that contains the config, the class, and the listSubscribe and calling it when I need it.

Hope this wasn't too verbose, and that it will be helpful to someone. If anyone wants specific questions answered, I would be more than happy to help.

Ya'll have a great day.
Douglas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.