简体   繁体   English

需要基本的MailChimp API帮助

[英]Basic MailChimp API help needed

I'm looking for some help figuring out why this basic script isn't working for me. 我正在寻找一些帮助,弄清楚为什么这个基本脚本不适合我。 I'm attempting to use the MailChimp API to have a custom form add new subscribers to a list. 我正在尝试使用MailChimp API将自定义表单添加到列表中。 The script isn't returning anything. 该脚本没有返回任何内容。

I know I'm using the API correctly, because when I type the url directly into my browser, the object is subscribed in MailChimp, which makes me think curl isn't set up right. 我知道我正在使用API​​,因为当我直接在我的浏览器中输入url时,该对象在MailChimp中订阅,这让我认为curl没有正确设置。 Any thoughts, please? 请问有什么想法吗?

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

It looks like I just had some silly syntax errors and I had mixed up GET and POST. 看起来我只是有一些愚蠢的语法错误,我混淆了GET和POST。 Fixed now. 现在修复了。 If anyone is interested, here is the code to sync a custom "Update Profile" form with multiple MailChimp lists (2 lists in this example). 如果有人感兴趣,这里是用于将自定义“更新配置文件”表单与多个MailChimp列表同步的代码(在此示例中为2个列表)。 If anyone has suggestions to make the code more efficient, that would be excellent as well. 如果有人建议使代码更有效,那也是很好的。

<?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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM