【问题标题】:how to add email address to mailchimp list using api call in php如何使用 php 中的 api 调用将电子邮件地址添加到 mailchimp 列表
【发布时间】:2014-04-17 06:27:19
【问题描述】:

我是 mailchimp 的新手。刚才我已经创建了帐户并获得了 api 密钥。我已经通过他们的 api 将电子邮件地址添加到列表中,但没有帮助。

我有联系我们的表格,当用户点击提交按钮时,我想在我的 mailchimp 数据库中添加用户的电子邮件 ID。

我也尝试过使用此代码,但收到 104 错误..

  $apikey = '***********-us3';
                $listID = '*******';
                $email = "********";
                $url = sprintf('https://us2.api.mailchimp.com/2.0/lists/subscribe&apikey=%s&id=%s&email_address=%s&output=json', $apikey, $listID, $email, $_SERVER['REMOTE_ADDR']);
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $data = curl_exec($ch);
                $arr = json_decode($data, true);
                curl_close($ch);
        if ($arr == 1) {
            echo 'Check now your e-mail and confirm your subsciption.';
        } else {
                            echo $arr['code'];
            switch ($arr['code']) {
                case 214:
                echo 'You are already subscribed.';
                break;
                // check the MailChimp API for more options
                default:
                echo 'Unkown error...';
                break;          
            }
        }

谁能建议我如何实现它?

提前致谢

【问题讨论】:

    标签: php mailchimp


    【解决方案1】:

    mailchimp 在https://bitbucket.org/mailchimp/mailchimp-api-php 提供了一个 PHP 包装器,它可以让生活变得轻松一百万倍。

    这里似乎发生的问题是您正在执行 GET 而不是 POST

    试试

    $apikey = '**********-us3';
    $listID = '******';
    $email  = "**************";
    $fields = array('apikey' => urlencode($apikey), 'id' => urlencode($listID), 'email_address' => urlencode($email), 'output' => 'json' );
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    $url = 'https://us2.api.mailchimp.com/2.0/lists/subscribe';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    $data = curl_exec($ch);
    $arr = json_decode($data, true);
    curl_close($ch);
    if ($arr == 1) {
        echo 'Check now your e-mail and confirm your subsciption.';
    } else {
                        echo $arr['code'];
        switch ($arr['code']) {
            case 214:
            echo 'You are already subscribed.';
            break;
            // check the MailChimp API for more options
            default:
            echo 'Unkown error...';
            break;          
        }
    }
    

    如果有帮助,请告诉我。如果没有,我可以看看我是否可以尝试自己设置一些东西,以便我可以测试一些代码。

    【讨论】:

    【解决方案2】:

    还有另一个流行的简单 API https://github.com/drewm/mailchimp-api

    use \DrewM\MailChimp\MailChimp;
    $MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1');
    
    $list_id = 'b1234346';
    $result = $MailChimp->post("lists/$list_id/members", [
                    'email_address' => 'davy@example.com',
                    'status'        => 'subscribed',
                ]);
    
    print_r($result);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-29
      • 2018-01-06
      • 2011-05-07
      • 2016-03-25
      • 2014-04-03
      • 2017-04-16
      • 2014-09-21
      • 1970-01-01
      相关资源
      最近更新 更多