繁体   English   中英

MailChimp 添加订阅者使用 API 3.0 PHP

[英]MailChimp Add Subscriber using API 3.0 PHP

我正在尝试添加一个 php 文件,该文件将一个新订阅者添加到我的 MailChimp 列表中。 这是我应该触发 php 文件以添加新订阅者的表单:

<form action="/scripts/freemonth_action.php" class="email_form_freemonth" method="post">
    <h3>Get your first month free</h3>
    <div class="form-group customised-formgroup"> <span class="icon-user"></span>
        <input type="text" name="full_name" class="form-control" placeholder="Name">
    </div>
    <div class="form-group customised-formgroup"> <span class="icon-envelope"></span>
        <input type="email" name="email" class="form-control" placeholder="Email">
    </div>
    <!--<div class="form-group customised-formgroup"> <span class="icon-telephone"></span>
        <input type="text" name="phone" class="form-control" placeholder="Phone (optional)">
    </div>-->
    <div class="form-group customised-formgroup"> <span class="icon-laptop"></span>
        <input type="text" name="website" class="form-control" placeholder="Website (optional)">
    </div>
    <!--<div class="form-group customised-formgroup"> <span class="icon-bubble"></span>
        <textarea name="message" class="form-control" placeholder="Message"></textarea>
    </div>-->
    <div>
        <br>
        <button style="margin: 0 auto" type="submit" class="btn btn-fill full-width">GET FREE MONTH<span class="icon-chevron-right"></span></button>
    </div>
</form>

这里是 freemonth_action.php:

<?php
session_start();
if(isset($_POST['submit'])){
    $name = trim($_POST['full_name']);
    $email = trim($_POST['email']);
    if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
        // MailChimp API credentials
        $apiKey = '6b610769fd3353643c7427db98d43ad6-us16';
        $listID = '0cf013d1d9';

        // MailChimp API URL
        $memberID = md5(strtolower($email));
        $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
        $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID;

        // member information
        $json = json_encode([
            'email_address' => $email,
            'status'        => 'subscribed',
            'merge_fields'  => [
                'NAME'     => $name,
            ]
        ]);

        // send a HTTP POST request with curl
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        $result = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        echo json_encode($result);

        // store the status message based on response code
        if ($httpCode == 200) {
            $_SESSION['msg'] = '<p style="color: #34A853">You have successfully subscribed to CodexWorld.</p>';
        } else {
            switch ($httpCode) {
                case 214:
                    $msg = 'You are already subscribed.';
                    break;
                default:
                    $msg = 'Some problem occurred, please try again.';
                    break;
            }
            $_SESSION['msg'] = '<p style="color: #EA4335">'.$msg.'</p>';
        }
    }else{
        $_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
    }
}

我什至不确定如何调试它,因为当我执行 echo $result (或类似的东西)时,我在页面上看不到任何内容或登录到控制台。 我也愿意接受任何使用 javascript 的建议,只要它仍然是 3.0 API。

您的订阅代码工作正常。 您没有看到任何结果的原因是isset($_POST['submit'])查找名称为“submit”而不是类型为“submit”的元素。 只需将name属性添加到您的按钮,它应该适合您:

<button style="margin: 0 auto" type="submit" name="submit" class="btn btn-fill full-width">GET FREE MONTH<span class="icon-chevron-right"></span></button>

此外,您应该对 API 密钥保密,以免其他人通过 API 访问您的 MailChimp 帐户。 我建议禁用您当前的密钥并创建一个新密钥。 有关更多详细信息,请参阅 MailChimp 关于API 密钥的知识库文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM