【问题标题】:Mailchimp Subscribe using API v3.0Mailchimp 使用 API v3.0 订阅
【发布时间】:2017-06-11 02:43:46
【问题描述】:

我正在尝试将电子邮件订阅到 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();
} ?>

【问题讨论】:

  • 这在 12 月被废弃,现在你必须使用 CURL 才能使用 3.0。看我的回答:)
  • 您需要通过查看服务器日志找出导致500 错误的原因,我的猜测是您使用的 PHP 版本低于 5.4 并且您的数组需要语法从[]固定到array()

标签: php mailchimp mailchimp-api-v3.0


【解决方案1】:

哦,伙计,当我遇到这个问题时,你不知道这个问题让我多么沮丧。

幸运的是,我发现了 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 );              

 ?>

我希望这会有所帮助。我为此苦苦挣扎了一段时间,直到我意识到如何正确地做到这一点。

【讨论】:

  • 伙计,非常感谢!这正是我所需要的。抱歉回复晚了,我正在完成我的项目。谢谢大家的帮助,你们太棒了。
猜你喜欢
  • 2016-03-20
  • 2015-10-17
  • 2015-11-20
  • 2016-01-03
  • 2016-01-11
  • 2013-11-09
  • 2015-08-13
  • 2015-06-02
  • 2017-05-15
相关资源
最近更新 更多