创建Mailchimp注册表单。
首先检查会员状态,然后根据需要添加或更新。
第一步是打开与Mailchimp 3.0 API的连接,获取成员状态并显示状态或错误。
这是我到目前为止所做的:
表单的HTML:
<!--Newsletter-->
<section title="Newsletter" id="newsletter" class="basicbox">
<h2>Sign Up for the Newsletter</h2>
<!-- Begin MailChimp Signup Form -->
<div id="newsletter-form">
<form id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" method="post" action="#" target="_blank">
<fieldset id="mc_embed_signup">
<p><input type="text" name="fname" class="required" placeholder="First name" title="It would be great to know your name."></p>
<p><input type="text" name="lname" class="required" placeholder="Last name" title="It would be great to know your last name."></p>
<p><input type="email" name="email" class="required email" placeholder="Your email address" title="Hey, I need your email address so I know where to send the newsletter." ></p>
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
<p><small>I respect your privacy and will not share your contact information.</small></p>
</fieldset>
</form>
</div>
</section>
<!--End mc_embed_signup-->
表单的JS(在结束</body
标记之前的同一HTML文档的底部:
<!-- Begin JavaScript -->
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.32/jquery.form.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.11.1/jquery.validate.min.js"></script>
<!-- validate newsletter form-->
<script>
$(function() {
$('#mc-embedded-subscribe-form').validate({
rules: {
fname: {
required: true,
minlength: 2
},
lname: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
},
messages: {
fname: {
required: "Oh come on, I really need to know your name.",
minlength: "Surely your name is longer than one character."
},
lname: {
required: "I'm normally not formal, but what is your surname?",
minlength: "Surely your name is longer than one character."
},
email: {
required: "Hey, I need your email address so I know where to send the newsletter."
},
},
submitHandler: function(form) {
$(form).ajaxSubmit({
url: 'process_mc_status.php',
success: function() {
$('#mc-embedded-subscribe-form').hide();
$('#newsletter-form').append("<p class='thanks'>Working on adding your awesome self to the list.</p>")
}
});
}
});
// How to now display the status?
// $('#newsletter-form').append("<p class='thanks'>"), $user_status, "</p>"
这是PHP,在process_mc_status.php中:
<?php
// Let's track errors in a log //
$fp = fopen(dirname(__FILE__).'errorlog.txt', 'w');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);
// Your Mailchimp account info //
// Data Center
$datacent = '{dataCenter}';
// List ID
$listid = '{listID}';
// API key
$mc_apikey = '{APIKey}';
// Email cleamup and member ID (leid) //
// Grab email address
$email = "strip_tags($_POST['email'])";
// Make email all lowercase
$email = strtolower($email);
// member ID, is email covert to md5
$leid = md5($email);
// first and last name variables //
// $fname = strip_tags($_POST['fname']);
// $lname = strip_tags($_POST['lname']);
$json_data = "";
// MailChimp API 3.0 URL for member status //
// /3.0/lists/{$listid}/members/{$leid}
$mc_url = "https://" . $datacent . ".api.mailchimp.com/3.0/lists/" . $listid . "/members/" . $leid;
// Get status from Mailchimp //
// create a new cURL resource
$ch = curl_init();
// Let's track errors in a log //
$fp = fopen(dirname(__FILE__).'errorlog.txt', 'w');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);
// Gete connection going //
curl_setopt($ch, CURLOPT_URL, $mc_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.$mc_apikey));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// $json_data contains the output string
// this method
// curl_setopt($ch,CURLOPT_GET,count($fields));
// curl_setopt($ch, CURLOPT_GETFIELDS, $json_data);
// OR
// this method
$json_data = curl_exec($ch);
// Check for errors
$info = curl_getinfo($ch);
$errno = curl_errno($ch);
if( $json_data === false || $errno != 0 ) {
// Do error checking
} else if($info['http_code'] != 200) {
// Got a non-200 error code.
// Do more error checking
}
// close cURL resource, and free up system resources
curl_close($ch);
// Get status from JSON //
$user_status = var_dump(json_decode($json_data->status));
echo $json_data;
echo "<br><br><br>";
echo 'User status is: '.$user_status;
?>
以上命中mailchimp服务器,它将所有用户数据转储到页面,但status的值为NULL。如何从会员信息中提取状态?同时尝试了$user_status = $json_data['status'];
,结果相同。
答案 0 :(得分:0)
您要为$user_status
变量指定字符串值'status'
。与json数据无关,它将始终回显'status'
。
请改为尝试:
// Get status from JSON //
json_decode($json_data, true);
$user_status = $json_data['status'];
然后只要你想要在屏幕上显示它就打印出来:
echo "<br><br><br>";
echo 'User status is: '.$user_status;