Mailchimp API 和不同的数组

Mail Chimp API and Different Arrays

好的。我正在使用 Aaron Walter 的脚本连接到 MailChimps API。

网站上有三个位置,我想 "track" 注册的来源。我能够使它与以下代码一起使用,但无法全神贯注于如何调整此代码,以便有 3 种不同的可能性。

function storeAddress(){

// Validation
if(!$_GET['email']){ return "No email address provided"; } 

if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
    return "Email address is invalid"; 
}

require_once('MCAPI.class.php');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('myAPIkey');

// 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 = "myLISTid";

//this line will allow me to see "headerBox" under sign up method within
// mailchimp so I know what form was used to sign up
$merge_vars = array('signup' =>'headerBox'); 

if($api->listSubscribe($list_id, $_GET['email'], $merge_vars) === true) {
    // It worked!   
    return 'Success! Check your email (and spam folder!) for your guide.';
}
else
{
    // An error ocurred, return error message   
    return 'Error: ' . $api->errorMessage;
}




}

我遇到的问题涉及这一行:

 $merge_vars = array('signup' =>'headerBox');

"signup" 将具有三个不同的值,具体取决于他们从哪个位置访问表单。我希望我已经足够清楚了。

编辑: 从上面的代码来看,为什么这样的事情行不通? 为每个表单放置一个隐藏的输入字段并检查给出的值...

if($_GET['signupMethod']=='headerBox') {
    $merge_vars = array('signup' =>'headerBox');
}
if($_GET['signupMethod']=='popUp') {
    $merge_vars = array('signup' =>'popUp');
}
if($_GET['signupMethod']=='footer') {
    $merge_vars = array('signup' =>'footer');
}

//continue with code
if($api->listSubscribe($list_id, $_GET['email'], $merge_vars) === true) {
    // It worked!   
    return 'Success! Check your email (and spam folder!) for your guide.';
}
else
{
    // An error ocurred, return error message   
    return 'Error: ' . $api->errorMessage;
}

正如您所说,您的表单方法是 GET 并且您创建了一个名为 signupMethod 的隐藏字段,请按以下方式操作:-

<?php
function storeAddress(){

// Validation
if(!$_GET['email']){ return "No email address provided"; } 

if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
    return "Email address is invalid"; 
}

require_once('MCAPI.class.php');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('myAPIkey');

// 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 = "myLISTid";

$merge_vars = ''; //define variable first and then assign values in next lines by checking it's value

if(isset($_GET['signupMethod']) && $_GET['signupMethod']=='headerBox') {
    $merge_vars = array('signup' =>'headerBox');
}
if(isset($_GET['signupMethod']) && $_GET['signupMethod']=='popUp') {
    $merge_vars = array('signup' =>'popUp');
}
if(isset($_GET['signupMethod']) && $_GET['signupMethod']=='footer') {
    $merge_vars = array('signup' =>'footer');
}


if($api->listSubscribe($list_id, $_GET['email'], $merge_vars) === true) {
    // It worked!   
    return 'Success! Check your email (and spam folder!) for your guide.';
}
else
{
    // An error ocurred, return error message   
    return 'Error: ' . $api->errorMessage;
}
?>