我有一个php脚本,可以使用批处理订阅将我的订阅者从我的wordpress用户列表更新到Mailchimp。(https://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php)
当我提交大约 400 条记录时,一切正常。 所有记录都被添加,我从 API 获得包含添加记录数等的返回。
如果我提交大约 600 个或更多(我有大约 730 个订阅者),所有记录都会添加到 Mailchimp,但 API 返回 FALSE。我用 === 假仔细检查了它,它是假的。 我没有收到任何错误 - 它只是返回 false(但所有记录都添加到 Mailchimp)。
Mailchimp说:"最大批大小因每条记录中的数据量而异,但您应该根据自己的经验将它们限制在5k - 10k记录。(https://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php)。
我离这个还很远,每条记录都被添加到 mailchimp 列表中就好了。 我只是没有从 API 获得回报。
我已将超时值增加到 5 分钟。 我还改用不同的记录,怀疑我可能有一条记录导致它搞砸了,但它在不同的记录上具有相同的行为。
我正在使用 DrewM 库与 Mailchimp API 版本 2.0 接口。我仔细检查以确保 DrewM 正在使用 post 进行请求,确实如此。(https://github.com/drewm/mailchimp-api/)
任何想法是什么原因造成的?
这是代码:
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/>";
}
在Mailchimp支持的帮助下,我能够找到并解决问题。
问题实际上出在 DrewM 包装器中。标头的内容长度部分显然在长调用中无法正常工作。 我删除了它,一切开始正常工作。
DrewM代码的原始部分(不起作用):
$result = file_get_contents($url, null, stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'user_agent' => 'PHP-MCAPI/2.0',
'method' => 'POST',
'header' => "Content-type: application/json'r'n".
"Connection: close'r'n" .
"Content-length: " . strlen($json_data) . "'r'n",
'content' => $json_data,
),
)));
更新的代码部分(工作):
$result = file_get_contents($url, null, stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'user_agent' => 'PHP-MCAPI/2.0',
'method' => 'POST',
'header' => "Content-type: application/json'r'n".
"Connection: close'r'n",
'content' => $json_data,
),
)));