Mailchimp API v3.0 如何实现订阅功能

3
我正在尝试使用PHP将电子邮件订阅到MailChimp列表中。我实际上不是后端开发人员,所以我卡在这里了 :(
我正在使用MailChimp帮助程序PHP库:https://github.com/drewm/mailchimp-api 我已经搜索了整个互联网,但只能得到500个内部服务器错误的状态。我已经在生产服务器上了。
<?php

include("./inc/MailChimp.php");
use \DrewM\MailChimp\MailChimp;

$api_key = "xxxxxxxxxxx-us13";
$list_id = "7xxxxxxx4";

$MailChimp = new MailChimp($api_key);

$result = $MailChimp->post("lists/$list_id/members", [
    "email_address" => $_POST["txt_mail"],
    'merge_fields'  => ['FNAME'=>$_POST["txt_name"], 'FPHONE'=>$_POST["txt_phone"], 'FMSG'=>$_POST["txt_message"]],
    "status"        => "subscribed"
]);

if ($MailChimp->success()) {
    echo "<h4>Thank you, you have been added to our mailing list.</h4>";
} else {
    echo $MailChimp->getLastError();
} ?>

1
这个已经在去年12月被弃用了,现在你必须使用CURL来使用3.0版本。请看我的回答 :) - scoopzilla
4
你需要查看服务器日志以找出导致“500”错误的原因。我猜想你使用的 PHP 版本低于 5.4,并且数组语法需要从“[]”更正为“array()”。请留意。 - cmorrissey
1个回答

6

哦,天啊,当我遇到这个问题时,你不知道我有多沮丧。

幸运的是,我找到了由Misha Rudrastyh提供的这个方便的工具,它与API 3.0非常配合。以下是要点:

因为我使用的是WordPress,所以我首先将下面的代码放入我的functions.php文件中(在此编辑了您的变量)

<?php
    function rudr_mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME'=> '', 'FPHONE'=> '', 'FMSG'=> '') ){
    $data = array(
         'apikey'        => $api_key,
         'email_address' => $txt_mail,
         'status'        => $status,
         'merge_fields'  => $merge_fields
    );
 $mch_api = curl_init(); // initialize cURL connection

    curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
    curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
    curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
    curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
    curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
    curl_setopt($mch_api, CURLOPT_POST, true);
    curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json

    $result = curl_exec($mch_api);
      return $result;
    }

然后我在表单处理中添加了变量字段:

<?php
    $email = $_POST['txt_mail'];
    $FNAME=$_POST['txt_name'];
    $FPHONE=$_POST['txt_phone'];
    $FMSG=$_POST['txt_message'];
    $status = 'pending'; // "subscribed" or "unsubscribed" or "cleaned" or "pending"
    $list_id = 'xxxxxxxxxxx-us13'; // where to get it read above
    $api_key = 'xxxxxxxxxxx-us13'; // where to get it read above
    $merge_fields = array('FNAME' => $FNAME, 'FPHONE' => $FPHONE, 'FMSG' => $FMSG);

    rudr_mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields );              

 ?>

我希望这可以帮到你。我曾经也遇到过类似的问题,直到我意识到如何正确解决它。


1
人啊,非常感谢你!这正是我所需要的。抱歉回复晚了,我在完成我的项目。谢谢大家的帮助,你们真棒。 - Fabio Sampaio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接