Today, We will tell you how to integrate MailChimp API with PHP.
Subscribers add to Mailchimp using their API using PHP cURL.
Mailchimp is a popular email marketing tool(service) that manages subscribers.
Using Mailchimp, you can send an email like announcements or offers.
Requirement
- API key from the Mailchimp
- List id from the Mailchimp
Customer data sent to mailchimp
$api_key = "*****************17-us7"; // update your key $list_id = "adsds554"; // update your lsit id $data_center = substr($api_key,strpos($api_key,'-')+1); $url = 'https://'. $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members'; $mergeData['email_address'] = '[email protected]'; $mergeData['status'] = 'subscribed'; //'subscribed' or 'pending' $mergeData['merge_fields'] = [ 'FNAME' => 'First Name', 'LNAME' => 'Last Name', "PHONE" => '7777777', "ADDRESS" => [ "addr1" => '32 road', "addr2" => 'newyork', "city" => 'mycity', "state" => 'london', "zip" => '7769', "country" => 'united Kindom' ] ]; $json = json_encode($mergeData); try { $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); $result = curl_exec($ch); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if (200 == $status_code) { echo "User added successfully to the Mailchimp."; } } catch(Exception $e) { echo $e->getMessage(); }
Delete a Subscriber from Mailchimp
$api_key = "*****************17-us8"; // update your key $list_id = "adsds554"; // update your lsit id $data_center = substr($api_key,strpos($api_key,'-')+1); $url = 'https://'. $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members/'. md5(strtolower($email)); try { $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); } catch(Exception $e) { echo $e->getMessage(); }
Get Mailchimp API Key
Mailchimp account. Under the user icon, select Account.
Click on Extra->API keys.
Under the “Your API keys” section, click on Create A Key.
Get Audience ID
Click on the Audience menu and then select the option Settings from the Manage Audience drop-down.
Under Settings click on the ‘Audience name and defaults’.
You will find your Audience ID from the next page.
Integrate MailChimp API with PHP
Like us on Facebook and Linkedin for more updates.
Access Denied For User ‘Phpmyadmin’@’Localhost’ (Using Password: YES)