我正在寻找一些帮助,弄清楚为什么这个基本脚本不适合我。我正在尝试使用MailChimp API将自定义表单添加到列表中。该脚本没有返回任何内容。
我知道我正在使用API,因为当我直接在我的浏览器中输入url时,该对象在MailChimp中订阅,这让我认为curl没有正确设置。有什么想法吗?
<?php
$apikey = 'xxx';
$listID = 'yyy';
$email = htmlspecialchars(stripslashes(trim($_POST['EMAIL'])));
$fname = htmlspecialchars(stripslashes(trim($_POST['FNAME'])));
$lname = htmlspecialchars(stripslashes(trim($_POST['LNAME'])));
if (!empty($_POST['EMAIL_UPDATES'])) {
$url = sprintf('http://us6.api.mailchimp.com/1.3/?method=listSubscribe&apikey=%s&id=%s&email_address=%s&merge_vars[OPTINIP]=%s&merge_vars[FNAME]=%s&merge_vars[LNAME]=%s&merge_vars[ZIP]=%s&output=json', $apikey, $listID, $email, $_SERVER['REMOTE_ADDR'], $fname, $lname);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$arr = json_decode($data, true);
if ($arr == 1) {
echo 'Check your e-mail and confirm your subscription.';
} else {
switch ($arr['code']) {
case 214:
echo 'You are already subscribed.';
break;
// check the MailChimp API for more options
default:
echo 'Unknown error.';
break;
}
}
}
?>
答案 0 :(得分:1)
看起来我只是有一些愚蠢的语法错误而且我混淆了GET和POST。现在修复了。如果有人感兴趣,这里是用于将自定义“更新配置文件”表单与多个MailChimp列表同步的代码(在此示例中为2个列表)。如果有人建议让代码更有效率,那也很好。
<?php
$apikey = 'xxx';
$listID1 = 'yyy';
$listID2 = 'zzz';
$email = htmlspecialchars(stripslashes(trim($_POST['EMAIL'])));
$fname = htmlspecialchars(stripslashes(trim($_POST['FNAME'])));
$lname = htmlspecialchars(stripslashes(trim($_POST['LNAME'])));
$mh = curl_multi_init();
if ($_POST['EMAILUPDATES'] == 'Yes') {
$url1 = 'http://us6.api.mailchimp.com/1.3/?method=listSubscribe&apikey='.$apikey.'&id='.$listID1.'&email_address='.$email.'&merge_vars[OPTINIP]='.$_SERVER['REMOTE_ADDR'].'&merge_vars[FNAME]='.$fname.'&merge_vars[LNAME]='.$lname.'&update_existing=true';
$ch1 = curl_init($url1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh,$ch1);
}
else if ($_POST['EMAILUPDATES'] == 'No') {
$url3 = 'http://us6.api.mailchimp.com/1.3/?method=listUnsubscribe&apikey='.$apikey.'&id='.$listID1.'&email_address='.$email;
$ch3 = curl_init($url3);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh,$ch3);
}
if ($_POST['BLOGUPDATES'] == 'Yes') {
$url2 = 'http://us6.api.mailchimp.com/1.3/?method=listSubscribe&apikey='.$apikey.'&id='.$listID2.'&email_address='.$email.'&merge_vars[OPTINIP]='.$_SERVER['REMOTE_ADDR'].'&merge_vars[FNAME]='.$fname.'&merge_vars[LNAME]='.$lname.'&update_existing=true';
$ch2 = curl_init($url2);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh,$ch2);
}
else if ($_POST['BLOGUPDATES'] == 'No') {
$url4 = 'http://us6.api.mailchimp.com/1.3/?method=listUnsubscribe&apikey='.$apikey.'&id='.$listID2.'&email_address='.$email;
$ch4 = curl_init($url4);
curl_setopt($ch4, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh,$ch4);
}
$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
if ($_POST['EMAILUPDATES'] == 'Yes') {
curl_multi_remove_handle($mh, $ch1);
}
else if ($_POST['EMAILUPDATES'] == 'No') {
curl_multi_remove_handle($mh, $ch3);
}
if ($_POST['BLOGUPDATES'] == 'Yes') {
curl_multi_remove_handle($mh, $ch2);
}
else if ($_POST['BLOGUPDATES'] == 'No') {
curl_multi_remove_handle($mh, $ch4);
}
curl_multi_close($mh);
?>