使用 Mailchimp API 3.0 获取并显示成员状态(或错误)
GET and display member status (or error) with Mailchimp API 3.0
正在创建 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(在同一 HTML 文档的底部,在结束 </body
标记之前:
<!-- 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'];
,结果相同。
您正在为 $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;
得到它的工作:
`// 从 JSON 获取状态 //
$json_data = json_decode(utf8_encode($json_data));
$user_status = $json_data->状态;
echo 'User status is: '.$user_status;`
正在创建 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(在同一 HTML 文档的底部,在结束 </body
标记之前:
<!-- 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'];
,结果相同。
您正在为 $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;
得到它的工作: `// 从 JSON 获取状态 // $json_data = json_decode(utf8_encode($json_data)); $user_status = $json_data->状态;
echo 'User status is: '.$user_status;`