我有一个包含以下字段的列表:email
,FNAME
和LNAME
。当我尝试通过Ajax发送merge_fields
时,Ajax会返回错误:
string(400)“{”type“:”http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/“,”title“:”资源无效“,”状态“:400,”详细信息“:”无法验证提交的资源。有关特定于字段的详细信息,请参阅'errors'数组。“,”instance“:”f311cff1-ec28-4003-8dde-007bc0001688“,”errors“:[{”field“:”merge_fields.FNAME“,”message“ :“数据与anyOf中描述的任何模式都不匹配。”}]}“
JS:
app.subscribe = function() {
$(document).on('submit', '.form-course', function(e) {
var fullName = $('#name').val(),
arrName = fullName.split(' '),
fname = arrName.slice(0, 1).join(' '),
lname = arrName.slice(1, arrName.length).join(' ');
var data = {
'email' : $('#email').val(),
"merge_fields": {
"FNAME": fname,
"LNAME": lname
}
};
console.log(data);
$.ajax({
url: url+'/wp-admin/admin-ajax.php?action=ux_subscribe',
method: 'POST',
data: data,
success: function(response) {
gtag('send', 'event', 'Modal', 'subscribed', 'Subscription');
fbq('track', 'CompleteRegistration');
$('#modal').fadeOut(function() {
// window.location.replace("./sucesso");
});
}, error: function(data) {
alert('ERRO! Tente novamente mais tarde!');
}
});
return false;
});
};
PHP:
function ux_subscribe() {
//GET via front-end form
$apiKey = 'XXX'; //Generate an API key via: Account > Extras > Api Keys
$listId = 'XXX'; //Find this via: Lists > List > Settings > List name and defaults
$email = $_POST['email']; //GET via front-end form
$fname = $_POST['fname']; //GET via front-end form
$lname = $_POST['lname']; //GET via front-end form
$auth = base64_encode( 'user:' . $apiKey );
echo $fname;
$json = json_encode([
'email_address' => $email,
'status' => "subscribed", // Choices include: "subscribed", "unsubscribed", "cleaned", "pending"
'merge_fields' => array(
'FNAME' => $fname,
'LNAME' => $lname
)
]);
$memberId = md5(strtolower( $email ));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Authorization: Basic ' . $auth]);
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_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
var_dump($result);
wp_die();
}
如果删除变量并放入一个字符串,它就可以工作:
$json = json_encode([
'email_address' => $email,
'status' => "subscribed", // Choices include: "subscribed", "unsubscribed", "cleaned", "pending"
'merge_fields' => array(
'FNAME' => 'hello',
'LNAME' => 'bye'
)
]);
有人知道为什么???
答案 0 :(得分:1)
您无法向API发送“空”值。
例如,这将起作用:
$fname = "joe";
$lname = "smith"
'merge_fields' => array(
'FNAME' => $fname,
'LNAME' => $lname
)
但是这会失败,请注意lname是如何定义的。
$fname = "joe";
'merge_fields' => array(
'FNAME' => $fname,
'LNAME' => $lname
)
因此,您需要一些简单的速记来提供“空”值。
$fname = empty($fname) ? " " : $fname;
$lname = empty($lname ) ? " " : $lname ;
'merge_fields' => array(
'FNAME' => $fname,
'LNAME' => $lname
)
我有一个包含20个段的大型数组,并且反复得到同样的错误,一旦我扔了一些正确的空()检查一切都很好......
答案 1 :(得分:0)
这很旧,但要指出您的javascript ajax调用发送此数据:
var data = {
'email' : $('#email').val(),
"merge_fields": {
"FNAME": fname,
"LNAME": lname
}
};
...但是您的PHP脚本正在尝试在以下位置查找发布变量:
$fname = $_POST['fname'];
$lname = $_POST['lname'];
只需将您的ajax数据更新为此:
var data = {
'email' : $('#email').val(),
'fname': fname,
'lname' : lname
};