Mailchimp Batch Subscribe 2.0 在 500 多条记录上返回 False (PHP) [英] Mailchimp Batch Subscribe 2.0 Returns False on 500+ records (PHP)

查看:20
本文介绍了Mailchimp Batch Subscribe 2.0 在 500 多条记录上返回 False (PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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.

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

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 说最大批量大小因每条记录中的数据量而异,但您应该将它们限制在 5k - 10k 记录,具体取决于您的经验."(https://apidocs.mailchimp.com/api/2.0/lists/批量订阅.php).

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).

我还没有做到这一点,每条记录都被添加到 mailchimp 列表中.我只是没有从 API 中得到回报.

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.

我已将超时值增加到 5 分钟.我还转而使用不同的记录,怀疑我可能有一个记录导致它搞砸了,但它对不同的记录有相同的行为.

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.

我正在使用 DrewM 库与 Mailchimp API 2.0 版进行交互.我再次检查以确保 DrewM 正在使用 post 请求,并且确实如此.(https://github.com/drewm/mailchimp-api/)

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?

代码如下:

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 支持的帮助下,我能够定位并解决问题.

With help from Mailchimp support, I was able to locate and solve the problem.

问题实际上出在 DrewM 包装器中.标题的内容长度部分显然在长时间调用时无法正常工作.我删除了它,然后一切正常.

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.

DrewM 代码的原始部分(不起作用):

Original section of DrewM code (not working):

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

更新的代码部分(工作):

Updated section of code (working):

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

这篇关于Mailchimp Batch Subscribe 2.0 在 500 多条记录上返回 False (PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆