Last active
July 21, 2023 18:06
-
-
Save toledox82/ffd05c6caad655b13368eb82f6132107 to your computer and use it in GitHub Desktop.
PHP cURL function to add subscriber to MailChimp API 3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = [ | |
'email' => $email, | |
'status' => 'subscribed', | |
'firstname' => $fname, | |
'lastname' => $lname | |
]; | |
function syncMailchimp($data) { | |
$apiKey = 'XXX'; | |
$listId = 'XXX'; | |
$memberId = md5(strtolower($data['email'])); | |
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1); | |
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId; | |
$json = json_encode([ | |
'email_address' => $data['email'], | |
'status' => $data['status'], // "subscribed","unsubscribed","cleaned","pending" | |
'merge_fields' => [ | |
'FNAME' => $data['firstname'], | |
'LNAME' => $data['lastname'] | |
] | |
]); | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey); | |
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_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); | |
$result = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
return $httpCode; | |
} | |
syncMailchimp($data); | |
?> |
works, nice, thanks 👍
Are you managing to update the tags of an existing contact using the PATCH method?
thank you
Sharibtanveer follow this article
https://usingphp.com/post/add-new-subscriber-in-a-mailchimp-list-api
Can you set an existing tag in the newly created subscriber?
Thanks 👍
great!
Thank you! 😀
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How this will pass
$emailname = $_REQUEST["emailname"];
$firstname = $_REQUEST["firstname"];
$lastname = $_REQUEST["lastname"];
Here
$data = [
'email' => $emailname,
'status' => 'subscribed',
'firstname' => $firstname,
'lastname' => $lastname
];
This not working dynamically.