3

I'm trying to implement a newsletter on my website using mailchimp API. I've tried the various suggestions made here, without success (I'm a complete newbie for that kind of things).

The code below should be working, however there's something that I don't understand when the user says "Here is an example using version 2.0 of Mailchimp API together with mailchimp-api (a minimal php abstraction class for dealing with the Mailchimp API)."

When I download the file there's no file called 'MailChimp.class.php". Just one called MailChimp.php. Do I need to rename it or something to get include('MailChimp.class.php'); work? And what about the other files part of mailchimp-api, can I just ignore them? Thanks

<?php

include('MailChimp.class.php');

$MailChimp = new MailChimp('API_KEY');
$result = $MailChimp->call('lists/subscribe', array(
    'id'                => 'LIST_ID',
    'email'             => array( 'email' => $_POST['email'] ),
    'merge_vars'        => array(
        'MERGE2' => $_POST['name'] // MERGE name from list settings
        // there MERGE fields must be set if required in list settings
    ),
    'double_optin'      => false,
    'update_existing'   => true,
    'replace_interests' => false
));

if( $result === false ) {
    // response wasn't even json
}
else if( isset($result->status) && $result->status == 'error' ) {
    // Error info: $result->status, $result->code, $result->name, $result->error
}

?>

HTML:

<div id="email">
        <span>Enter your email to sign up</span>
        <form action="subscribe.php" id="invite" method="POST">
            <input type="text" placeholder="your@email.com" name="email" id="address" data-validate="validate(required, email)"/>
            <button type="submit">&#187;</button>
        </form>
        <span id="result"></span>
    </div>
Community
  • 1
  • 1
Greg
  • 3,025
  • 13
  • 58
  • 106

1 Answers1

1

I accessed the link you mentioned, https://github.com/drewm/mailchimp-api, and in the description, the author shows a piece of code as example.

$MailChimp = new \Drewm\MailChimp('abc123abc123abc123abc123abc123-us1');

I guess you just need to include the file (don't need to rename it) and then access the library using his namespace "\Drewn".

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
hlscalon
  • 7,304
  • 4
  • 33
  • 40