Mailchimp 批量订阅 2.0 Returns 500 多条记录错误 (PHP)

Mailchimp Batch Subscribe 2.0 Returns False on 500+ records (PHP)

我有一个 php 脚本来使用批量订阅将我的订阅者从我的 wordpress 用户列表更新到 Mailchimp。 (https://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php)

当我提交大约 400 条记录时一切正常。添加了所有记录,我从 API 中得到一个 return,其中包含添加的记录数等

如果我提交大约 600 个或更多(我有大约 730 个订阅者),所有记录都会添加到 Mailchimp,但 API returns FALSE。我用 === false 仔细检查了它,它是 false。我没有收到任何错误 - 它只是 returns false(但所有记录都已添加到 Mailchimp)。

Mailchimp 说 "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)。

我还差得远呢,每条记录都被添加到 mailchimp 列表中了。我只是没有从 API.

中得到 return

我已将超时值增加到 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 包装器中。 header 的 content-length 部分在长时间调用时显然无法正常工作。我删除了它,一切都开始正常工作了。

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,
            ),
        )));