JavierCabot
JavierCabot

Reputation: 33

Mailchimp API: Add subscribers to segments

I'm making a script to add subscribers to mailchimp list and assigns them to a segment. Adding subscribers to lists works fine, but when I try to assign to a segment, mailchimp api returns an error.

This is the code

    <?php
$api_key = "MYAPYKEY";
$list_id = "MYLIST";
$segment_id = "SEGMENT_D";

$merge_vars = Array( 
        'FNAME' => 'James', 
    );

require('src/Mailchimp.php');
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => '[email protected]' ),$merge_vars,'','false','true');

echo '<pre>';
var_dump($subscriber);
echo '</pre>';

$segment = $Mailchimp_Lists->staticSegmentMembersAdd($list_id, $segment_id, array("email" => "[email protected]"));

echo '<pre>';
var_dump($segment);
echo '</pre>';

?>

And here result

array(3) {
  ["email"]=>
  string(16) "[email protected]"
  ["euid"]=>
  string(10) "cdb3aa63cb"
  ["leid"]=>
  string(9) "132001013"
}
array(3) {
  ["success_count"]=>
  int(0)
  ["error_count"]=>
  int(1)
  ["errors"]=>
  array(1) {
    [0]=>
    array(3) {
      ["email"]=>
      string(16) "[email protected]"
      ["code"]=>
      int(232)
      ["error"]=>
      string(59) "There is no record of the email address "m" in your account"
    }
  }
}

First var_dump it's ok result when add email to list, and second var_dump it's error when adding it to segment.

The Segment exists. I see that API only takes first letter of email.

Upvotes: 0

Views: 1746

Answers (1)

CDL
CDL

Reputation: 1

Try

$segment = $Mailchimp_Lists->staticSegmentMembersAdd($list_id, $segment_id, array(array("email" => "[email protected]")));

Need to nest the email array Found this out from this page MailChimp API member-info issue

Worked for me when I had exactly the same problem as you.

Upvotes: 0

Related Questions