多列表订阅中的Mailchimp误报

时间:2016-01-22 10:05:30

标签: php mailchimp

这是我的代码,我用它来订阅用户选择的多个Mailchimp列表,并通过ajax请求发布到此文件。这是代码:

因此,由于某种原因,这会从Mailchimp返回成功,但它实际上并没有订阅我输入的列表。我已经确认列表ID是正确的,字段也是。

(我删除了对实际ID的所有引用)

<?php

    include 'MailChimp.php';

    $mailchimp = new MailChimp('MYAPI_KEY'); //I have my code in here

    $newsletter_keyarray = array(
        "List one" => "listoneid",
        "List two" => "listtwoid",
        "List three" => "listthreeid",
        "List four" => "listfourid"
    );

    $any_errors = false;

    foreach ($_POST['lists'] as $key => $list) {
        if(in_array($list, $newsletter_keyarray)){

            $merge_vars_array = array(
                'EMAIL' => $_POST['email']
            );

            if($list == "List one"){
                $merge_vars_array['MMERGE1'] = $_POST['fname'];
                $merge_vars_array['MMERGE2'] = $_POST['sname'];
                $merge_vars_array['MMERGE3'] = $_POST['org'];
            }else if($list == "List two"){
                $merge_vars_array['FNAME'] = $_POST['fname'];
                $merge_vars_array['LNAME'] = $_POST['sname'];
                $merge_vars_array['MMERGE4'] = $_POST['job'];
                $merge_vars_array['MMERGE5'] = $_POST['org'];
            }else if($list == "List three"){
                $merge_vars_array['FNAME'] = $_POST['fname'];
                $merge_vars_array['LNAME'] = $_POST['sname'];
                $merge_vars_array['MMERGE3'] = $_POST['org'];
                $merge_vars_array['MMERGE4'] = $_POST['job'];
            }else if($list == "List four"){
                $merge_vars_array['FNAME'] = $_POST['fname'];
                $merge_vars_array['LNAME'] = $_POST['sname'];
                $merge_vars_array['MMERGE4'] = $_POST['job'];
                $merge_vars_array['MMERGE5'] = $_POST['org'];
            }

            $mailResults = $mailchimp->call('lists/subscribe', array(
                'id' => $newsletter_keyarray[$list],        
                'email' => array('email' => $_POST['email']),
                'merge_vars' => $merge_vars_array,
                'double_optin' => false
            ));
            if(isset($mailResults['status']) && $mailResults['status'] == 'error'){
                $any_errors = true;
            }
        }
    }

    //enter api and userdetails here
    $response_array = array();

    if($any_errors){
        header('Content-type: application/json');
        $response_array['status'] = 'error';
        if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
            $response_array['message'] = 'The email you have entered is not valid';
        }else if(isset($mailResults['error']) && $mailResults['error'] != ""){
            $response_array['message'] = $mailResults['error'];
        }
        echo json_encode($response_array);
    }else{
        header('Content-type: application/json');
        $response_array['status'] = 'success';
        echo json_encode($response_array);
    }

?>

1 个答案:

答案 0 :(得分:0)

我认为这有点愚蠢。

问题是in_array,它比较值而不是键。所以最后我使用了array_key_exists