我是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;
}
}
任何人都可以建议我如何实现它吗?
提前致谢
答案 0 :(得分:5)
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;
}
}
如果有帮助,请告诉我。如果没有,我可以看看我是否可以尝试自己设置,以便我可以测试一些代码。
答案 1 :(得分:1)
还有另一种Popular Simple 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);