将自定义数据从联系表单字段发布到 Mailchimp 列表 API 不起作用

Posting custom data from contact form fields to Mailchimp list with API not working

我正在尝试将 $POST_[values] 从我的表单推送到我的 Mailchimp 联系人列表,但我可以推送的唯一两个值是“电子邮件”和“状态”(即“已订阅").

我阅读了所有文档,但无法使用提供的合并字段。

HTML


<div class="input-box">
                    <span>First Name</span>
                    <input type="text" name="FNAME" value="FNAME" tabindex="1">
                </div>
                <div class="input-box">
                    <span>Last Name</span>
                    <input type="text" name="LNAME" value="LNAME" tabindex="2">
                </div>
                <div class="input-box">
                    <span>Email*</span>
                    <input type="email" name="email2" tabindex="3" required>
                </div>
                <div class="input-box">
                    <span>Company*</span>
                    <input type="text" tabindex="4" name="MMERGE9" value="MMERGE9" required>
                </div>
                <input id="submit-download" type="submit" name="submit2" tabindex="5" disabled></input>

PHP


include('./mailchimp/MailChimp.php'); 
$MailChimp = new MailChimp('XXXX');
$list_id = 'XXX';
$email = $_POST['email2'];
$MERGE9 = $_POST['MMERGE9'];
$result = $MailChimp->post("lists/$list_id/members", [
                'email_address' => $email,
                'status'        => 'subscribed',
                'MERGE9'        => $MERGE9,

我尝试了 Mailchimp 提供的所有变体:MERGE9、MMERGE9、|MERGE9|

我使用这个库:https://github.com/drewm/mailchimp-api

我也尝试使用 curl 命令列出合并字段,但我无法让它工作:

https://mailchimp.com/developer/api/marketing/list-merges/

注意:此问题的重点是使用自定义 HTML 表单 post 使用 MC API 的 MailChimp 列表的新成员;与验证、安全、spam/reCaptcha 和其他重要考虑因素相关的代码已被省略。我不建议在生产中单独使用此代码。

原始问题:当post将一个新的'member'与他们的API[使用post("lists/$list_id/members")],只存储了两个字段,'status' 和 'email_address',忽略了我希望也存储的所有 merge_fields .

解决方案:'merge_fields' 必须使用 MC API.[=] 作为数组 提交 11=]

为了解决我的问题,我发现使用 GET 使用 curl 直接使用 API 进行一些实验很有用,因为 drawm 的 mailchimp-api 库很棒,但它隐藏了一点内部运作。

PHP

<?php
include './../mailchimp/MailChimp.php'; // update this \path\to MailChimp.php from drewm mailchimp library (https://github.com/drewm/mailchimp-api);
use \DrewM\MailChimp\MailChimp; // do not change/edit this line;
if(isset($_POST['submit'])) { //edit as needed the name of the submit field in your form;
    $MailChimp = new MailChimp($key); //update $key with your MailChimp API key; should look like 3856ojd298g5q82c2213453a98172346-xx22; can be found under >>Top Right Corner dropdown (displaying your name)>>Account>>Extras>>API Keys>>Create a key [keep this key private as gives partial API access to your account]
    $email = $_POST['email']; // update field name as needed;
    $fname = $_POST['FNAME']; // update field name as needed;
    $lname = $_POST['LNAME']; // update field name as needed;
    $company = $_POST['COMPANY']; // update field name as needed;
    $result = $MailChimp->post("lists/$list_id/members", [      // update $list_id with your list_id; should look like this 'js83js93jm'; can be found under MailChimp.com>>Audience>>Audience Dashboard>>Manage Audience dropdown>>Settings>>Audience name and defaults>>Audience ID
                'email_address' => $email, // this field is a MC API default, don't edit;
                'merge_fields' => [
                    'FNAME' => $fname,
                    'LNAME' => $lname,
                    'MMERGE9' => $company,
                    ],
                'status'        => 'subscribed', // this field is a MC API default, don't edit, although you could possibly remove it if you don't want to add the subscribed status at this point (verify in the docs);
                
            ]);
?>