MailChimp使用API 3.0 PHP添加订户
我正在尝试添加一个php文件,将一个新订户添加到我的MailChimp列表中。以下是我的表单,它将触发php文件以添加新订户:MailChimp使用API 3.0 PHP添加订户,php,html,mailchimp,Php,Html,Mailchimp,我正在尝试添加一个php文件,将一个新订户添加到我的MailChimp列表中。以下是我的表单,它将触发php文件以添加新订户: <form action="/scripts/freemonth_action.php" class="email_form_freemonth" method="post"> <h3>Get your first month free</h3> <div class="form-group customised
<form action="/scripts/freemonth_action.php" class="email_form_freemonth" method="post">
<h3>Get your first month free</h3>
<div class="form-group customised-formgroup"> <span class="icon-user"></span>
<input type="text" name="full_name" class="form-control" placeholder="Name">
</div>
<div class="form-group customised-formgroup"> <span class="icon-envelope"></span>
<input type="email" name="email" class="form-control" placeholder="Email">
</div>
<!--<div class="form-group customised-formgroup"> <span class="icon-telephone"></span>
<input type="text" name="phone" class="form-control" placeholder="Phone (optional)">
</div>-->
<div class="form-group customised-formgroup"> <span class="icon-laptop"></span>
<input type="text" name="website" class="form-control" placeholder="Website (optional)">
</div>
<!--<div class="form-group customised-formgroup"> <span class="icon-bubble"></span>
<textarea name="message" class="form-control" placeholder="Message"></textarea>
</div>-->
<div>
<br>
<button style="margin: 0 auto" type="submit" class="btn btn-fill full-width">GET FREE MONTH<span class="icon-chevron-right"></span></button>
</div>
</form>
第一个月免费
免费一个月
下面是freemonth_action.php:
<?php
session_start();
if(isset($_POST['submit'])){
$name = trim($_POST['full_name']);
$email = trim($_POST['email']);
if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
// MailChimp API credentials
$apiKey = '6b610769fd3353643c7427db98d43ad6-us16';
$listID = '0cf013d1d9';
// MailChimp API URL
$memberID = md5(strtolower($email));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID;
// member information
$json = json_encode([
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => [
'NAME' => $name,
]
]);
// send a HTTP POST request with curl
$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);
echo json_encode($result);
// store the status message based on response code
if ($httpCode == 200) {
$_SESSION['msg'] = '<p style="color: #34A853">You have successfully subscribed to CodexWorld.</p>';
} else {
switch ($httpCode) {
case 214:
$msg = 'You are already subscribed.';
break;
default:
$msg = 'Some problem occurred, please try again.';
break;
}
$_SESSION['msg'] = '<p style="color: #EA4335">'.$msg.'</p>';
}
}else{
$_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
}
}
您的订阅代码工作正常。您没有看到任何结果的原因是isset($\u POST['submit'])
查找名为'submit'而不是类型为'submit'的元素。只需将name
属性添加到您的按钮中,它就应该适用于您:
<button style="margin: 0 auto" type="submit" name="submit" class="btn btn-fill full-width">GET FREE MONTH<span class="icon-chevron-right"></span></button>
免费月
此外,您应该对API密钥保密,以便其他人无法通过API访问您的MailChimp帐户。我建议禁用当前密钥并创建一个新密钥。有关更多详细信息,请参阅MailChimp的知识库文章。我不确定MailChimp API规则,但通常您会json\u decode()
结果,而不是最后的json\u encode()
。哦,好的,谢谢。这会打印到文档中还是打印到控制台日志中?这会将MailChimp发送回的数据从JSON转换为数组。然后,您可以操纵数组以打印到控制台或其他任何地方。只是提醒一下,您可能希望编辑您的问题并删除api密钥(如果它是真实的)。我添加了name=“submit”,但它仍然没有将电子邮件添加到mail chimp中的我的订阅列表中。我甚至没有在mail chimp api中看到成功的api调用log@Tom当您回显会话['msg']
时是否能够收到消息?我发现了问题。Joel H.关于我忘记输入name=“submit”的说法是对的,另一个问题是我收到一条错误消息:“无法在第43行将blah转换为字符串”,这就是echo json_encode($result)代码>。在我的html中添加name='submit'并去掉这行php之后,api现在可以工作了。