編集:
この回答を投稿してから、MailChimpはAPIのバージョン2と3をリリースしました。2017年以降、サポートされるバージョンはバージョン3のみになります。テストする機会があり次第、この回答をAPIバージョン3に更新します。
MailChimp API v3.0
このページの上部にある通知によると、 2016年以降はAPIの以前のバージョンはすべてサポートされなくなります。
私のソリューションでは、APIを処理するためにバックグラウンドでPHPを使用し、Ajaxを容易にするためにjQueryを使用しています。
1)APIv3.0をサポートするPHPラッパーをダウンロードします。この記事の執筆時点では、v3.0をサポートする最新のMailChimpドキュメントに公式にリストされているものはありませんが、GitHubにいくつかリストされているので、これを選択しました。
2)独自のAPIキーとリストIDを使用して次のPHPファイルを作成し、store-address.php
手順1のラッパーと同じディレクトリに配置します。ラッパーのドキュメントに従うことを忘れないでください、しかしそれらはすべてこれにかなり似ているようです。
<?php // for MailChimp API v3.0
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
ファイルは次のようになります。
<form id="signup" action="index.html" method="get">
First Name: <input type="text" name="fname" id="fname" />
Last Name: <input type="text" name="lname" id="lname" />
email Address (required): <input type="email" name="email" id="email" />
<input type="submit" id="SendButton" name="submit" value="Submit" />
</form>
<div id="message"></div>
<script src="jquery.min.js"></script>
<script>
$(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', // <- IMPORTANT
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;
});
});
</script>
MailChimp APIバージョン1:
(元の回答)
しばらくいじくり回した後、jQueryでPHPの例を使用しているサイトを見つけました。それから、基本的なサインアップフォームを含むjQueryを使用して簡単なHTMLページを作成することができました。PHPファイルはバックグラウンドで「隠され」ており、ユーザーには表示されませんが、jQueryは引き続きアクセスして使用できます。
1)ここからPHP 5 jQueryの例をダウンロードします...(編集:リンクは無効です。ただし、重要な部分は、ここで入手できるPHPの公式APIラッパーのみです。)
http://apidocs.mailchimp.com/downloads/mcapi-simple-subscribe-jquery.zip
PHP 4しかない場合は、MCAPIのバージョン1.2をダウンロードして、MCAPI.class.php
上記の対応するファイルを置き換えてください。
http://apidocs.mailchimp.com/downloads/mailchimp-api-class-1-2.zip
2)APIキーとリストIDをstore-address.php
ファイルの適切な場所に追加して、Readmeファイルの指示に従います。
3)ユーザーの名前やその他の情報を収集することもできます。store-address.php
対応するマージ変数を使用して、ファイルに配列を追加する必要があります。
名、名前、メールの種類も収集したstore-address.php
ファイルは次のようになります。
<?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 '<b>Error:</b> ' . $api->errorMessage;
}
}
// If being called via ajax, autorun the function
if($_GET['ajax']) {
echo storeAddress();
}
4)HTML / CSS/jQueryフォームを作成します。PHPページにある必要はありません。
私のindex.html
ファイルは次のようになります。
<form id="signup" action="index.html" method="get">
First Name: <input type="text" name="fname" id="fname" />
Last Name: <input type="text" name="lname" id="lname" />
email Address (required): <input type="email" name="email" id="email" />
HTML: <input type="radio" name="emailtype" value="html" checked="checked" />
Text: <input type="radio" name="emailtype" value="text" />
<input type="submit" id="SendButton" name="submit" value="Submit" />
</form>
<div id="message"></div>
<script src="jquery.min.js"></script>
<script>
$(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;
});
});
</script>
必要なもの...
上記のように構築されたindex.htmlまたは同様のもの。jQueryを使用すると、外観とオプションは無限に広がります。
MailchimpサイトのPHPサンプルの一部としてダウンロードされ、APIKEYおよびLISTIDで変更されたstore - address.phpファイル。他のオプションのフィールドを配列に追加する必要があります。
MailchimpサイトからダウンロードしたMCAPI.class.phpファイル(PHP5の場合はバージョン1.3、PHP 4の場合はバージョン1.2)。それをstore-address.phpと同じディレクトリに配置するか、store- address.php内のURLパスを更新して見つけられるようにする必要があります。