MailChimp 订阅 PHP Class 表单合并字段

MailChimp Subscribe PHP Class Form Merge Fields

您好,我正在使用 MailChimp 订阅 PHP Class Tatwerat-Team 的表格 (link)

我正在创建一个 html 表单,该表单使用 PHP 和其他一些表单在邮件黑猩猩列表上创建新订阅者。
当它只是 FNAME 和 EMAIL 时它工作正常但是一旦我添加验证中断的其他字段并且我不确定为什么?

表格:

<?php
$config = array(
    #Mailchimp details
    "Mailchimp_ApiKey" => "API ID REMOVED FOR POST",
    "Mailchimp_ListID" => "LIST ID REMOVED FOR POST",
);
if ($_POST) {
    // First Name and Last Name Required
    require 'includes/MailChimp-Class.php';
    $MailChimp = new MailChimp($config['Mailchimp_ApiKey'], $config['Mailchimp_ListID'], TRUE);
    $MailChimp->subscribed($_POST['fname'], NULL, $_POST['email'], $_POST['pcode'], $_POST['tnumber'], $_POST['ntype']);
    echo '<div class="well">' . $MailChimp->message() . '</div>';
    $output = $MailChimp->message();
    if ($output == 'Thank you for subscribe our newsletter') {
        echo '';
    } else {
        echo 'There has been a problem. Please try again later';
    }
}
?>
<form class="subscribe" method="post" id="mc-embedded-subscribe-form">
    <div class="field">
        <input type="text" name="fname"  placeholder="Name" required>
    </div>
    <div class="field">
        <input type="email" name="email"  placeholder="Email" required>    
    </div>
    <div class="field">
        <input type="text" name="pcode" placeholder="Postcode" required>   
    </div>
    <div class="field">
        <input type="text" name="tnumber" placeholder="Telephone Number" required>  
    </div>
    <div class="field">
        <select id="ddl2" name="ntype" required>
            <option value="" disabled selected>Choose netting type</option>
            <option value="cricket">Cricket Ballstop Netting</option>
            <option value="golf">Golf Ballstop Netting</option>
            <option value="football">Football Ballstop Netting</option>
        </select>  
    </div>
    <div class="field">
        <button type="submit" title="Submit" class="button"><span> <i class="fa fa-arrow-right"></i> Download Now for FREE</span></button>
    </div>
</form>
<!--End mc_embed_signup-->
<script>
    $("#mc-embedded-subscribe-form").validate({
        rules: {
            field: {
                required: true,
                email: true,
            }
        }
    });
</script>

Mailchimp Class PHP

<?php

error_reporting(E_ALL);

/*
 * 
 * MailChimp Subscribe PHP Class Form 
 * 
 * Let public visitors to subscribe your newsletter 
 * 
 * PHP Version 5.x
 *
 * Author Tatwerat-Team 
 * 
 * Author-Account http://themeforest.net/user/tatwerat-team 
 * 
 * Version 1.0
 *
 */

class MailChimp {

public $Key;
public $ListID;
public $Error;
public $Email;
public $FName;
public $LName;
public $Status = 'subscribed';
public $FullData;

public function __construct($API_Key, $List_ID, $Full_Data = TRUE) {
    $this->Key = $API_Key;
    $this->ListID = $List_ID;
    $this->FullData = $Full_Data;
}

public function subscribed($fname, $lname, $email, $pcode, $tnumber, $ntype,) {
    $this->FName = $fname;
    $this->LName = $lname;
    $this->Email = $email;
    $this->PCode = $pcode;
    $this->TNumber = $tnumber;
    $this->NType = $ntype;
    $this->message();
    if (!$this->Error)
        $this->curlData($this->apiUrl(), $this->Key, $this->jsonData(), 'PUT');
}

public function apiUrl() {
    $apiKey = $this->Key;
    $listId = $this->ListID;
    $memberId = $memberId = md5(strtolower($this->Email));
    $getapi = substr($this->escape($apiKey), strpos($this->escape($apiKey), '-') + 1);
    return 'https://' . $this->escape($getapi) . '.api.mailchimp.com/3.0/lists/' . $this->escape($listId) . '/members/' . $this->escape($memberId);
}

public function jsonData() {
    return json_encode([
        'email_address' => $this->escape($this->Email),
        'status' => $this->escape($this->Status),
        'merge_fields' => [
            'NAME' => $this->escape($this->FName),
            'PCODE' => $this->escape($this->PCode),
            'TNUMBER' => $this->escape($this->TNumber),
            'NTYPE' => $this->escape($this->NType),
        ]
    ]);
}

public function curlData($url, $apiKey, $json, $type, $get = FALSE) {
    $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, $type);
    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);
    if ($get)
        echo var_dump(json_decode($result));
    else
        $this->Error = ($httpCode != 200) ? "[error-" . $httpCode . "]" : '';
}

public function validate() {
    if (empty($this->FName) and $this->FullData) {
        $this->Error = 'First name required';
    } elseif (empty($this->PCode)) {
        $this->Error = 'Email required';
    } elseif (empty($this->TNumber)) {
        $this->Error = 'Email required';
    } elseif (empty($this->NType)) {
        $this->Error = 'Email required';
    } elseif (empty($this->Email)) {
        $this->Error = 'Email required';
    } elseif (!filter_var($this->Email, FILTER_VALIDATE_EMAIL)) {
        $this->Error = 'Invalid your email';
    }
}

public function escape($string) {
    return htmlspecialchars(trim($string), ENT_QUOTES, 'UTF-8');
}

public function message() {
    $this->validate();
    if (!$this->Error) {
        return 'Thank you for subscribe our newsletter';
    } else {
        if ($this->Error == "[error-0]") {
            $this->Error = 'Bad request';
            return;
        } elseif ($this->Error == "[error-400]") {
            $this->Error = 'Invalid your email';
            return;
        } elseif ($this->Error == "[error-401]") {
            $this->Error = 'Invalid API key';
            return;
        } elseif ($this->Error == "[error-404]") {
            $this->Error = 'Invalid list ID';
            return;
        }
        return $this->Error;
    }
}

}

我今天成功解决了这个问题,请看下面。

新建MailChimp_Class.php文件

<?php

error_reporting(E_ALL);

/*
 * 
 * MailChimp Subscribe PHP Class Form 
 * 
 * Let public visitors to subscribe your newsletter 
 * 
 * PHP Version 5.x
 *
 * Author Tatwerat-Team 
 * 
 * Author-Account http://themeforest.net/user/tatwerat-team 
 * 
 * Version 1.0
 *
 */

class MailChimp {

public $Key;
public $ListID;
public $Error;
public $Email;
public $FName;
public $LName;
public $PCode;
public $TNumber;
public $NType;
public $Status = 'subscribed';
public $FullData;

public function __construct($API_Key, $List_ID, $Full_Data = TRUE) {
    $this->Key = $API_Key;
    $this->ListID = $List_ID;
    $this->FullData = $Full_Data;
}

public function subscribed($fname, $lname, $email, $pcode, $tnumber, $ntype) {
    $this->FName = $fname;
    $this->LName = $lname;
    $this->Email = $email;
    $this->PCode = $pcode;
    $this->TNumber = $tnumber;
    $this->NType = $ntype;
    $this->message();
    if (!$this->Error)
        $this->curlData($this->apiUrl(), $this->Key, $this->jsonData(), 'PUT');
}

public function apiUrl() {
    $apiKey = $this->Key;
    $listId = $this->ListID;
    $memberId = $memberId = md5(strtolower($this->Email));
    $getapi = substr($this->escape($apiKey), strpos($this->escape($apiKey), '-') + 1);
    return 'https://' . $this->escape($getapi) . '.api.mailchimp.com/3.0/lists/' . $this->escape($listId) . '/members/' . $this->escape($memberId);
}

public function jsonData() {
    return json_encode([
        'email_address' => $this->escape($this->Email),
        'status' => $this->escape($this->Status),
        'merge_fields' => [
            'FNAME' => $this->escape($this->FName),
            'PCODE' => $this->escape($this->PCode),
            'TNUMBER' => $this->escape($this->TNumber),
            'NTYPE' => $this->escape($this->NType),
        ]
    ]);
}

public function curlData($url, $apiKey, $json, $type, $get = FALSE) {
    $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, $type);
    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);
    if ($get)
        echo var_dump(json_decode($result));
    else
        $this->Error = ($httpCode != 200) ? "[error-" . $httpCode . "]" : '';
}

public function validate() {
    if (empty($this->FName) and $this->FullData) {
        $this->Error = 'Please Enter Your Name';
    } elseif (empty($this->Email)) {
        $this->Error = 'Please Enter Your Email';
    } elseif (!filter_var($this->Email, FILTER_VALIDATE_EMAIL)) {
        $this->Error = 'Please Enter a Valid Email';
    } elseif (empty($this->PCode) and $this->FullData) {
        $this->Error = 'Please Enter Your Postcode';
    } elseif (empty($this->TNumber) and $this->FullData) {
        $this->Error = 'Please Enter Your Telephone Number';
    } elseif (empty($this->NType) and $this->FullData) {
        $this->Error = 'Please Select a Netting Type';
    }
}

public function escape($string) {
    return htmlspecialchars(trim($string), ENT_QUOTES, 'UTF-8');
}

public function message() {
    $this->validate();
    if (!$this->Error) {
        return 'Thank you for subscribing to our newsletter';
    } else {
        if ($this->Error == "[error-0]") {
            $this->Error = 'Bad request';
            return;
        } elseif ($this->Error == "[error-400]") {
            $this->Error = 'Invalid your email';
            return;
        } elseif ($this->Error == "[error-401]") {
            $this->Error = 'Invalid API key';
            return;
        } elseif ($this->Error == "[error-404]") {
            $this->Error = 'Invalid list ID';
            return;
        }
        return $this->Error;
    }
}

}

新建HTML

<!-- Begin MailChimp Signup Form -->

<?php
    $config = array(
        #Mailchimp details
        "Mailchimp_ApiKey" => "insert apikey",
        "Mailchimp_ListID" => "insert list id",
    );
    if ($_POST) {
        // First Name and Last Name Required
        require 'includes/MailChimp-Class.php';
        $MailChimp = new MailChimp($config['Mailchimp_ApiKey'], $config['Mailchimp_ListID'], TRUE);
        $MailChimp->subscribed($_POST['fname'], NULL, $_POST['email'], $_POST['pcode'], $_POST['tnumber'], $_POST['ntype']);
        echo '<div class="well">' . $MailChimp->message() . '</div>';

        $output = $MailChimp->message();
        if ($output == 'Thank you for subscribing to our newsletter') {
        echo '<script type="text/javascript">
                   window.top.location.href = "http://example.com"
              </script>';
        } else { echo ''; }

    }
    ?>
    <form class="subscribe" method="post" id="mc-embedded-subscribe-form">
        <div class="field">
            <input type="text" name="fname"  placeholder="Name">
        </div>
        <div class="field">
            <input type="email" name="email"  placeholder="Email">    
        </div>
        <div class="field">
            <input type="text" name="pcode" placeholder="Postcode">   
        </div>
        <div class="field">
            <input type="text" name="tnumber" placeholder="Telephone Number">  
        </div>
        <div class="field">
            <select id="ddl2" name="ntype">
                <option value="Choose netting type" disabled selected>Choose netting type</option>
                <option value="Cricket Ballstop Netting">Cricket Ballstop Netting</option>
                <option value="Golf Ballstop Netting">Golf Ballstop Netting</option>
                <option value="Football Ballstop Netting">Football Ballstop Netting</option>
            </select>  
        </div>
        <div class="field">
            <button type="submit" title="Submit" class="button"><span> <i class="fa fa-arrow-right"></i> Download Now for FREE</span></button>
        </div>
    </form>
<!--End mc_embed_signup-->

<script>
    $( "#mc-embedded-subscribe-form" ).validate({
      rules: {
        field: {
          required: true,
          email: true,
        }
      }
    }); 
</script>