编程语言
首页 > 编程语言> > 如何使用php中的api调用将电子邮件地址添加到mailchimp列表中

如何使用php中的api调用将电子邮件地址添加到mailchimp列表中

作者:互联网

我是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;          
            }
        }

任何人都可以建议我如何实现它?

提前致谢

解决方法:

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;          
    }
}

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

标签:php,mailchimp
来源: https://codeday.me/bug/20190825/1716442.html