【问题标题】:MailChimp API: Batch Check Subscription StatusMailChimp API:批量检查订阅状态
【发布时间】:2015-03-19 07:12:10
【问题描述】:

检查一批电子邮件地址以及它们是否订阅了特定列表的最佳方法是什么?我当前的方法是对每个电子邮件地址进行 API 调用,但是当计数大于 50 时,这会变得非常慢。

我正在使用 V2.0,使用 PHP 方法,因为我是在 CakePHP 上开发的。

 //Determine MailChimp Subscription
 $args = array(
    'id' => $this->apiKeys['mailchimp_list_ID'],
    'emails' => array(1 => array('email' => $customer['User']['username']))
 );
 $mailChimpQuery = $this->mailChimp('member-info', $args);
 $mailChimpStatus = ($mailChimpQuery['success_count'] == 1 ? 1 : 0);
 $customer['Customer']['subscribed'] = $mailChimpStatus;

另一个控制器中的mailChimp调用是:

public function mailChimp($action, $args=array()) {

        $args['apikey'] = $this->apiKeys['mailchimp_api'];
        $url = 'https://us8.api.mailchimp.com/2.0/lists/' . $action .'.json';

        if (function_exists('curl_init') && function_exists('curl_setopt')){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
            $result = curl_exec($ch);
            curl_close($ch);
        } else {
            $json_data = json_encode($args);
            $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,
                    ),
                )));
        }

        return  $result ? json_decode($result, true) : false;
    }

【问题讨论】:

    标签: php api mailchimp


    【解决方案1】:

    仅供参考,我想出了自己的解决方案:

    1. 在数据库中创建一个包含所有电子邮件地址的数组
    2. 创建一个 for 循环,从该数组中获取 50 个电子邮件地址段,并使用 member-info (https://apidocs.mailchimp.com/api/2.0/lists/member-info.php) 运行批量检查
    3. 操作 API 调用的返回数组以仅提取电子邮件地址以及它是否订阅了列表。
    4. 在for循环中,将每50个电子邮件地址的返回值添加到一个新数组中,然后添加到它的末尾;您将拥有数据库中所有电子邮件地址的数组,其中包含相应的真/假值,具体取决于它们是否订阅了该列表。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 2014-07-12
      • 2019-05-16
      • 2016-03-20
      • 2019-10-09
      • 2016-09-04
      • 2017-11-21
      • 2018-05-31
      • 2013-11-09
      相关资源
      最近更新 更多