我正在寻找一些帮助来弄清楚为什么这个基本脚本不适合我。我正在尝试使用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;
}
}
}
?>