gpt4 book ai didi

ajax - merge_fields 错误 400 - Mailchimp api v3

转载 作者:行者123 更新时间:2023-12-05 01:43:40 25 4
gpt4 key购买 nike

我有一个包含这些字段的列表:emailFNAMELNAME。当我尝试通过 Ajax 发送 merge_fields 时,Ajax 返回错误:

string(400) "{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"f311cff1-ec28-4003-8dde-007bc0001688","errors":[{"field":"merge_fields.FNAME","message":"Data did not match any of the schemas described in 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'
)
]);

有人知道为什么吗???

最佳答案

您不能向 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 个段的大数组,一遍又一遍地遇到同样的错误,一旦我投入一些适当的 empty() 检查一切都很好......

关于ajax - merge_fields 错误 400 - Mailchimp api v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48845001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com