如何解决使用API无法将联系人表格字段中的自定义数据发布到Mailchimp列表中[已解决]
我正在尝试将$ POST_ [values]从表单中推送到Mailchimp的联系人列表中,但我只能推送的两个值是“ email”和“ status”(已“订阅”) )。
我已阅读所有文档,但无法使其与提供的合并字段一起使用。
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表单通过MC API将新成员发布到MailChimp列表中;与验证,安全性,垃圾邮件/ reCaptcha和其他重要注意事项有关的代码已被省略。我不建议在生产中单独使用此代码。
原始问题:在使用其API向MC“ list_id”发布新的“成员” [使用post(“ lists / $ list_id / members”)]时,只有两个字段“状态” '和'email_address'被存储,而忽略了我也希望存储的所有merge_fields。
解决方案:“ merge_fields”必须使用MC API作为数组提交。
为了解决我的问题,我发现使用GET使用curl来直接对API进行一些实验很有用,因为drewm的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',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);
]);
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。