我在使用v3 API在列表中创建新成员时遇到问题。我正在发送带有json数据的发布请求:

网址:

https://us10.api.mailchimp.com/3.0/lists/<list-id>/members

header :
   Content-type: application/json
   Authorization: apikey <my-api-key>

杰森的 body :
{
  "status": "pending",
  "email_address": "[email protected]",
  "merge_fields": {
    "FNAME": "John",
    "LNAME": "Smith",
    "REFERRER": "referrer",
    "REFERRAL": "referral"
  }
}

它基于api文档和教程https://teamtreehouse.com/library/mailchimp-api/mailchimp-api/adding-new-members-to-your-list。但是每个响应看起来像:
{
  "type": "http://kb.mailchimp.com/api/error-docs/400-invalid-resource",
  "title": "Invalid Resource",
  "status": 400,
  "detail": "Your merge fields were invalid.",
  "instance": "",
  "errors": [
    {
      "field": "FNAME",
      "message": "Please enter a value"
    },
    {
      "field": "LNAME",
      "message": "Please enter a value"
    },
    {
      "field": "REFERRAL",
      "message": "Please enter a value"
    },
    {
      "field": "REFERRER",
      "message": "Please enter a value"
    }
  ]
}

我究竟做错了什么? MailChimp API是否有问题?

最佳答案

将用户添加到列表时,您必须添加列表中“必填”的所有字段。要查看您的字段,请登录黑猩猩,然后单击“列表”,然后进入您的列表。从“设置”下拉列表中,选择“列表字段,然后| MERGE |标签”。

mailchimp - 使用MailChimp API v3.0创建新的订户验证错误-LMLPHP

新闻稿是我添加的另一个自定义必填字段。如果我不发送,我将收到您的错误消息,因为这是必填字段。所有其他字段都需要添加到merge_fields数组中,例如“NEWSLETTER”。

$dataCenter ='us10'; //last part of the api key
$auth = base64_encode( 'user:'.$apikey );
$data = array(
   'apikey'        => $apikey,
   'email_address' => $email,
   'status'        => 'subscribed',
   'merge_fields'  => ['NEWSLETTER' => '1']
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/f3e05535e8/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('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_data);
$result = curl_exec($ch);

如果我的表单中有更多自定义字段,则可以这样添加它们:
'merge_fields'  => [
    'NEWSLETTER' => '1',
    'ANOTHERFIELD' => 'this is example data',
]

邮件黑猩猩文档说,它需要一个'merge_fields'对象,当数组转换为json时,它将成为一个json对象。

http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/

关于mailchimp - 使用MailChimp API v3.0创建新的订户验证错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32967371/

10-12 13:28