Mailchimp PHP api v.2,用户已存在或取消订阅返回错误

时间:2016-10-20 13:38:28

标签: php mailchimp

我正在使用Mailchimp v.2 API PHP包装器来处理订阅... https://bitbucket.org/mailchimp/mailchimp-api-php但是如果订阅者已经存在于订阅者或订阅者已取消订阅,则会返回错误,我想要知道一种方法我可以将其编码到其中任何一种情况存在的地方,它将继续运行代码而不显示错误。如果这是一个简单的问题,我道歉。

$api_key = "XXXXXXXXX";
$list_id = "XXXXXX";
require('Mailchimp.php');
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$merge_vars = array('FNAME'=>htmlentities($_POST['stripeFirstName']), 'LNAME'=>htmlentities($_POST['stripeLastName']) );
    $subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => htmlentities($_POST['stripeEmail']) ), $merge_vars );

1 个答案:

答案 0 :(得分:0)

您需要使用trycatch。 Mailchimp对于出了什么问题非常冗长。

try {
    $result = $Mailchimp_Lists->subscribe(
        $list_id,
        array(/* ... */), // primary subscriber data
        array(/* ... */), // merge fields
        'html', // email-type preference
        false // double-opt-in
    );
    // Action for successful subscribe attempt.
} catch (\Exception $e) {
    if ($e instanceof \Mailchimp_List_AlreadySubscribed) {
        // In case they are already subscribed:
        $errors[] = $e->getMessage();
    } else {
        // In case something else went wrong.
        $errors[] = 'There was an error adding your email to the list. Would you mind trying again?';
    }

    // Debug time!
    var_dump($errors);
}