Mail Chimp API和不同的阵列

时间:2015-06-04 18:46:26

标签: php mailchimp

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

网站上有三个位置,我想“跟踪”注册的来源。我能够使用以下代码来解决这个问题,但是我无法理解如何调整此代码以便有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;
}

1 个答案:

答案 0 :(得分:1)

正如您所说,您的表单方法为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;
}
?>