Amna Ahmed
Amna Ahmed

Reputation: 1964

Mailchimp Api Webhooks

I am integrating with mailchimp APi using their wrapper class. I have configured the webhook in my mailchimp dashboard and the file which will get webhook response has this on top

 if(isset($_POST['type'])){
 $yes=$_POST['data']['email'];

 $querynewsubscrip="INSERT into newslettersubscrips SET optemail='$yes'";
 $resultnewsubscripxx=mysql_query($querynewsubscrip) or die('Query failed: ' .  mysql_error());

 }

Is this what i need to catch the response from Mailchimp? What i assume is, webhooks send me data through post, type tells me what was the action and data array has all the data. Please guide

Upvotes: 1

Views: 5555

Answers (2)

Misha Rudrastyh
Misha Rudrastyh

Reputation: 873

According to you code, it looks like you would like just to subscribe a user when a appropriate MailChimp webhook is fired.

All you have to do is to check $_POST['type'] variable sent by MailChimp, if it is equal to the required status, do the stuff, example:

if( isset( $_POST['type'] ) && $_POST['type'] == 'subscribe' ) {

    $yes=$_POST['data']['email'];

    $querynewsubscrip="INSERT into newslettersubscrips SET optemail='$yes'";
    $resultnewsubscripxx=mysql_query($querynewsubscrip) or die('Query failed: ' .  
    mysql_error());

}

So, the only thing that was missing in your code is $_POST['type'] == 'subscribe' in the condition. Because if you didn't add it, all the other webhook types will be also connected to your code.

Official MailChimp docs about webhooks: https://developer.mailchimp.com/documentation/mailchimp/guides/about-webhooks/ and a tutorial https://rudrastyh.com/mailchimp-api/webhooks.html#processing-webhooks

Upvotes: 0

Erik
Erik

Reputation: 581

Mailchimp have a guide here: http://apidocs.mailchimp.com/webhooks/downloads/#php And some example code here: http://apidocs.mailchimp.com/webhooks/downloads/webhooks.phps

That should explain what you need to do.

Upvotes: 2

Related Questions