本文介绍了Mailchimp批量订阅2.0返回上500+记录(PHP)假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,以使用批处理订阅从我的话preSS用户列表为Mailchimp更新我的用户。 ()

当我提交约400条记录,一切工作正常。所有记录被添加,我也得到从与添加的记录数量等方面的API回报。

如果我提交约600以上(我有大约730户),所有记录被添加到Mailchimp,但API返回FALSE。我是双检查它===假的,那是假的。我没有得到任何错误 - 它只是返回false(但所有记录将被添加到Mailchimp)

Mailchimp说:最大批量大小变化基于数据的每条记录的金额,但应在5K上限它们 - 10K的记录,这取决于你的经验。 ()。

我无处接近,每一个记录添加到列表mailchimp就好。我只是不明白从API返回的。

我已经在我的超时值提高到5分钟。我也转向使用不同的记录,怀疑我可能有东西被它造成的混乱创造了一项纪录,但它有不同的记载相同的行为。

我使用的DrewM库Mailchimp API 2.0版本接口。我双重检查,以确保DrewM使用后的要求,它的作用。 ()

任何想法是什么原因造成的?

下面是code:

 函数mailchimpdailyupdate(){
    或者set_time_limit(300);
      $ API = get_mc_api();
      $ mcListId = get_mc_mailing_list();
      $ MailChimp =新\\ Drewm \\ MailChimp($ API);

...

 的foreach($ blogusers为$ USER){
      $用户信息= get_userdata($用户可和SEQ ID);
      $位置= ... // code获得位置
      $ merge_vars =阵列(
                   FNAME'=> $ userinfo-> FIRST_NAME,
                   L-NAME => $ userinfo->姓氏,
                   MMERGE3'=> $ userinfo-> USER_LOGIN,//用户名
                   MMERGE6'=> $位置//位置
                   );      $批[] =数组(
                    电子邮件=>阵列('邮件'= GT; $用户自> USER_EMAIL)
                    merge_vars'=> $ merge_vars
                    );
  } //结束的foreach   // mailchimp电话
            $ RETVAL = $ MailChimp->调用(名单/批量订阅,阵列(
                'ID'=> $ mcListId,//在这里您mailchimp列表ID
                批=> $批次,
                update_existing'=>真正
              )
            );  如果($ RETVAL === FALSE){
    回声Mailchimp API返回false
  }  回声补充说:。 $ RETVAL ['ADD_COUNT']。 < BR />中;
  回声更新:。 $ RETVAL ['update_count']。 < BR />中;
  回声错误:。 $ RETVAL ['ERROR_COUNT']。 < BR />中;
}


解决方案

从Mailchimp的支持帮助下,我能够找到并解决问题。

这个问题实际上是在DrewM包装。
头部的内容长度部分显然没有对长时间通话正常工作。我删除它,一切都开始正常工作。

DrewM code(不工作)的原件部分:

  $结果=的file_get_contents($网址,空,stream_context_create(阵列(
            'HTTP'=>阵列(
                PROTOCOL_VERSION'=> 1.1,
                USER_AGENT'=> PHP-MCAPI / 2.0',
                '方法'= GT; POST,
                '头'=> 内容类型:应用程序/ JSON \\ r \\ n。
                                      连接:关闭\\ r \\ n。
                                      内容长度:。 strlen的($ json_data)。 \\ r \\ n,
                '内容'=> $ json_data,
            )
        )));

code(工作)的更新部分:

  $结果=的file_get_contents($网址,空,stream_context_create(阵列(
            'HTTP'=>阵列(
                PROTOCOL_VERSION'=> 1.1,
                USER_AGENT'=> PHP-MCAPI / 2.0',
                '方法'= GT; POST,
                '头'=> 内容类型:应用程序/ JSON \\ r \\ n。
                                      连接:关闭\\ r \\ n,
                '内容'=> $ json_data,
            )
        )));

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

        $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批量订阅2.0返回上500+记录(PHP)假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 06:03