【发布时间】:2017-11-18 01:28:16
【问题描述】:
您好,我正在使用 Tatwerat-Team 的 MailChimp 订阅 PHP 类表单 (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 类 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;
}
}
}
【问题讨论】:
-
检查你的公共函数 subscribed($fname, $lname, $email, $pcode, $tnumber, $ntype,) 删除逗号
-
缩进和段落的一些设置
标签: javascript php mailchimp mailchimp-api-v3.0