html创建mail,html – 使用他们的API创建一个基本的MailChimp注册表单

已编辑:

自从发布此回答MailChimp已发布版本2& 3的API。版本3将是从2017年开始的唯一受支持的版本。一旦我有机会测试,我会更新这个答案API版本3。

MailChimp API v3.0

我的解决方案在后台使用PHP来处理API,并使用jQuery来简化Ajax。

1)下载支持API v3.0的PHP包装器。在撰写本文时,最新的MailChimp文档中没有官方列出支持v3.0的文档,但是有一些文档在GitHub上列出,因此我选择了this one。

2)使用您自己的API密钥和列表ID创建以下PHP文件store-address.php,然后将其放在与步骤一的包装器相同的目录中。记住遵循你的包装器的文档,但是他们都看起来很相似。

include('MailChimp.php'); // path to API wrapper downloaded from GitHub

use \DrewM\MailChimp\MailChimp;

function storeAddress() {

$key = "xxxxxxxxxxxxxxx-us1";

$list_id = "xxxxxx";

$merge_vars = array(

'FNAME' => $_POST['fname'],

'LNAME' => $_POST['lname']

);

$mc = new MailChimp($key);

// add the email to your list

$result = $mc->post('/lists/'.$list_id.'/members', array(

'email_address' => $_POST['email'],

'merge_fields' => $merge_vars,

'status' => 'pending' // double opt-in

// 'status' => 'subscribed' // single opt-in

)

);

return json_encode($result);

}

// If being called via ajax, run the function, else fail

if ($_POST['ajax']) {

echo storeAddress(); // send the response back through Ajax

} else {

echo 'Method not allowed - please ensure JavaScript is enabled in this browser';

}

3)创建你的HTML / CSS / JavaScript(jQuery)表单(它不需要在PHP页面上,访问者永远不会看到在后台使用PHP)。

响应是JSON,所以你必须正确处理它。

这里是我的index.html文件看起来像:

First Name:

Last Name:

email Address (required):

$(document).ready(function() {

$('#signup').submit(function() {

$("#message").html("Adding your email address...");

$.ajax({

url: 'inc/store-address.php', // proper url to your "store-address.php" file

type: 'POST', //

data: $('#signup').serialize() + '&ajax=true',

success: function(msg) {

var message = $.parseJSON(msg),

result = '';

if (message.status === 'pending') { // success

result = 'Success! Please click the confirmation link that will be emailed to you shortly.';

} else { // error

result = 'Error: ' + message.detail;

}

$('#message').html(result); // display the message

}

});

return false;

});

});

MailChimp API版本1:

(原答案)

在摸索了一段时间后,我发现一个网站使用PHP示例与jQuery。从那里,我能够创建一个简单的HTML页面与jQuery包含基本的注册表单。 PHP文件“隐藏”在后台,用户从来没有看到它们,jQuery仍然可以访问&使用。

1)在这里下载PHP 5 jQuery示例…(编辑:链接已经死了,但是,唯一重要的部分是PHP的官方API包装器可用HERE.)

如果你只有PHP 4,只需下载MCAPI的1.2版,并替换上面相应的MCAPI.class.php文件。

2)按照自述文件中的说明,通过将API密钥和列表ID添加到store-address.php文件的适当位置。

3)您可能还想收集用户的姓名和/或其他信息。您必须使用相应的合并变量将数组添加到store-address.php文件。

这里是我的store-address.php文件看起来像我也收集的名字,姓氏和电子邮件类型:

function storeAddress() {

require_once('MCAPI.class.php'); // same directory as store-address.php

// grab an API Key from http://admin.mailchimp.com/account/api/

$api = new MCAPI('123456789-us2');

$merge_vars = Array(

'EMAIL' => $_GET['email'],

'FNAME' => $_GET['fname'],

'LNAME' => $_GET['lname']

);

// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/

// Click the "settings" link for the list - the Unique Id is at the bottom of that page.

$list_id = "123456a";

if ($api->listSubscribe($list_id, $_GET['email'], $merge_vars , $_GET['emailtype'])) {

// It worked!

return 'Success!  Check your inbox or spam folder for a message containing a confirmation link.';

} else {

// An error ocurred, return error message

return 'Error:  ' . $api->errorMessage;

}

}

// If being called via ajax, autorun the function

if($_GET['ajax']) {

echo storeAddress();

}

4)创建你的HTML / CSS / jQuery表单。它不需要在PHP页面上。

这里是我的index.html文件看起来像:

First Name:

Last Name:

email Address (required):

HTML:

Text:

$(document).ready(function() {

$('#signup').submit(function() {

$("#message").html("Adding your email address...");

$.ajax({

url: 'inc/store-address.php', // proper url to your "store-address.php" file

data: $('#signup').serialize() + '&ajax=true',

success: function(msg) {

$('#message').html(msg);

}

});

return false;

});

});

必需件…

> index.html如上构建或类似。使用jQuery,外观和选项是无止境的。> store-address.php文件作为PHP示例的一部分下载到Mailchimp网站上,并使用您的API KEY和LIST ID进行修改。您需要将其他可选字段添加到数组。>从Mailchimp站点下载的MCAPI.class.php文件(PHP 5版本1.3或PHP 4版本1.2)。将它放在与store-address.php相同的目录中,或者必须更新store-address.php中的url路径,以便它可以找到它。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值