In this tutorial we can see how to subscribe to the mailchimp subscribe list using php programmatically via api,
For this you need a mailchimp account, api key and list id
you can see the tutorials to get api-key and list id using links
we are using PHP and curl for posting request to mailchimp servers, follow the steps and code, code is simple a php file with subscribe form in that you can enter firstname lastname and emails to subscribe to mailing list of mailchimp.
<?php
if($_POST){
$data = [
'email' => $_POST['email'],
'status' => 'subscribed',
'firstname' => $_POST['firstname'],
'lastname' => $_POST['lastname']
];
// NOTE: status having 4 Option --"subscribed","unsubscribed","cleaned","pending"
$res = syncMailchimp($data);
if($res == 200){
echo '<div class="alert alert-success" role="alert">Subscribed Successfull</div>';
}else{
echo '<div class="alert alert-danger" role="alert">Unable to Subscribe at the moment, try again later</div>';
}
}
function syncMailchimp($data)
{
$apiKey = 'YOUR_API_KEY;
$listId = 'YOUR_LIST_ID';
$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'],
'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;
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<title>Subscribe To Mailchimp</title>
</head>
<body class="container">
<h3>Subscribe To Mailchimp</h3>
<form action="#" method="POST" id="suscribeForm">
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" class="form-control" id="firstname" name="firstname" placeholder="First name" required>
</div>
<div class="form-group">
<label for="lastname">Last Name</label>
<input type="text" class="form-control" id="lastname" name="lastname" placeholder="Last Name" required>
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required>
</div>
<input type="submit" class="btn btn-primary" value="Submit"/>
</form>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>
Just replace your API KEY and LIST ID and your are all set.
Note: we are using bootstrap and additional js to to look pretty you can avoid that if you don;t like, thanks
- Log in to post comments