简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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