简体   繁体   中英

Mailchimp API - Subscribe to list

I'm working on adding a script to my site for a MailChimp subscribe form. I think I have everything setup right but when I hit the subscribe button I'm getting a blank page.

Here is the script I have currently

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

require_once 'Mailchimp.php';

$apikey = "XXXXXXXXXXXXXXX";
$Mailchimp = new Mailchimp($apikey);

if (!empty($_POST)) {

    $id = "XXXXXXXXXX";
    $email = array(
        'email' => trim($_POST['email'])
    );

    $result = $Mailchimp->$lists->subscribe($id, $email, $double_optin=false, $replace_interests=false);

    var_dump($result);

}

echo "TESTING";

So I'm not getting the $result variable or "TESTING echo'd right now, so I assume I must be doing something simple wrong. Anyone see anything obvious? I believe I'm using the correct default JSON format. (keys have been X'd out, but the one's I'm using are correct)

Any help is much appreciated.

Thanks!!

EDIT: I have updated the code to something I believe to be more correct, but it still isn't working. I could really use some help on this.

Here's how I've handled AJAX email submissions in the past for MailChimp's API (MCAPI):

define("MC_API_KEY", "Your mailchimp API key");
define("MC_API_LIST", "The list ID to subscribe user to");
define("EMAIL_TO", "email address in case subscription fails");

require "MailChimp.API.class.php";

function json($error = true, $message = "Unknown error") { 
  die(json_encode(array("error" => $error, "message" => $message)));
}

if(!empty($_POST)) {
  $email = !empty($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ? $_POST['email'] : false;

  if($email !== false) {    
    $api = new MCAPI(MC_API_KEY);
    $result = $api->listSubscribe(MC_API_LIST, $email);

    if($api->errorCode || !$result) {
      if(isset($api->errorCode) && $api->errorCode == 214) { // already subscribed
        json(true, "You are already subscribed!");
      } else {
        $error = "Unable to save user email via MailChimp API!\n\tCode=".$api->errorCode."\n\tMsg=".$api->errorMessage."\n\n";
        $headers = "From: your@email.com\r\nReply-to: <{$email}>\r\nX-Mailer: PHP/".phpversion();

        mail(EMAIL_TO, "Newsletter Submission FAIL [MC API ERROR]", "{$error}Saved info:\nFrom: {$email}\n\nSent from {$_SERVER['REMOTE_ADDR']} on ".date("F jS, Y \@ g:iA e"), $headers);

        json(false, "Thank you - your email will be subscribed shortly!");
      }
    } else {
      json(false, "Thanks - A confirmation link has been sent to your email!");
    }
  } else {
    json(true, "Please enter your valid email address");
  }
} else json();

SOLVED:

Here is the correct code - hopefully this will help others looking to use the new api -

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

require_once 'Mailchimp.php';

$apikey = "XXXXXXXXXXXXXXXXXXX";
$Mailchimp = new Mailchimp($apikey);

if (!empty($_POST)) {

    $id = "XXXXXXXXXX";
    $email = array(
        'email' => trim($_POST['email'])
    );

    $result = $Mailchimp->lists->subscribe($id, $email, $merge_vars=null, $double_optin=false, $replace_interests=false);

}

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