MailChimp PHP
MailChimp is the most famous online email marketing software and is offered free of cost. It allows the user to subscribe and send newsletter in bulk to them. It also manages your subscriber’s lists. MailChimp makes it easy to integrate email signup form in web application and website also. MailChimp provides lots of forms APIs and functions.
In this example, we will explain how can we subscribe the user directly to MailChimp list using CURL.
First challenge we face while working on curl is …
Where to submit the request? In this case we need to submit the request to the following URL
https://X.api.mailchimp.com/2.0/lists/subscribe.json
But in this case you need to replace X.
Please check the following steps, to change this URL
Please check your API key. It would have some value which comes after – .
eg.
API KEY : XXXXXXXXXXXXXXXXXXXXd4fc0f71c21e-us13
In this API KEY the data which is coming after – is us13
So we need to replace X in “https://X.api.mailchimp.com/2.0/lists/subscribe.json” with “us13“.
So the URL will become https://us13.api.mailchimp.com/2.0/lists/subscribe.json
$time = date(“Y-m-d H:i:s”);
$merge_vars=array(
‘OPTIN-TIME’=>$time, // Must be something readable by strtotime…
‘FNAME’=>ucwords(“Gurpal”),
‘LNAME’=>ucwords(“Singh”)
);$send_data=array(
’email’=>array(’email’=>’YOUR EMAIL ID’),
‘apikey’=>”YOUR API KEY”,
‘id’=>”LIST ID FROM MAILCHIMP”,
‘merge_vars’=>$merge_vars,
‘double_optin’=>false,
‘update_existing’=>true,
‘replace_interests’=>false,
‘send_welcome’=>false,
’email_type’=>”html”,
);$submit_url=”https://us13.api.mailchimp.com/2.0/lists/subscribe.json“; // This URL will depend upon your API key.
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$submit_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($send_data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo $result=curl_exec($ch);
curl_close($ch);
$mcdata=json_decode($result);
?>
Leave a comment