I have a php script to update my subscribers from my wordpress userlist to Mailchimp using batch-subscribe. (https://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php)
Everything works fine when I submit about 400 records. All records are added, and I get a return from the API with the number of added records, etc.
If I submit about 600 or more (I have about 730 subscribers), all records are added to Mailchimp, but the API returns FALSE. I double checked it with === false, and it is false. I get no errors -- it just returns false (but all records are added to Mailchimp).
Mailchimp says "Maximum batch sizes vary based on the amount of data in each record, though you should cap them at 5k - 10k records, depending on your experience." (https://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php).
I'm nowhere close to that, and every record is being added to the mailchimp list just fine. I just don't get the return from the API.
I've increased my timeout value to 5 minutes. I also switched to using different records, suspecting I might have had a record with something that was causing it to mess up, but it had the same behavior with different records.
I'm using the DrewM library to interface with Mailchimp API version 2.0. I double checked to make sure DrewM is using post for the request, and it does. (https://github.com/drewm/mailchimp-api/)
Any ideas what is causing this?
Here is the code:
function mailchimpdailyupdate () {
set_time_limit(300);
$api = get_mc_api();
$mcListId = get_mc_mailing_list();
$MailChimp = new \Drewm\MailChimp($api);
...
foreach ( $blogusers as $user ) {
$userinfo = get_userdata( $user->ID );
$location = ...//code to get location
$merge_vars = array(
'FNAME'=> $userinfo->first_name,
'LNAME'=> $userinfo->last_name,
'MMERGE3'=> $userinfo->user_login, //username
'MMERGE6'=> $location //location
);
$batch[] = array(
'email' => array('email' => $user->user_email),
'merge_vars' => $merge_vars
);
} //end foreach
//mailchimp call
$retval = $MailChimp->call('lists/batch-subscribe', array(
'id' => $mcListId, // your mailchimp list id here
'batch' => $batch,
'update_existing' => true
)
);
if ($retval === false) {
echo "Mailchimp API returned false";
}
echo 'Added: ' . $retval['add_count'] . "<br/>";
echo 'Updated: ' . $retval['update_count'] . "<br/>";
echo 'Errors: ' . $retval['error_count'] . "<br/>";
}
With help from Mailchimp support, I was able to locate and solve the problem.
The issue was actually in the DrewM wrapper. The content-length section of the header was apparently not working correctly on long calls. I removed it, and everything began working fine.
Original section of DrewM code (not working):
Updated section of code (working):