Check whether email is subscribed to list in MailChimp API 3.0 using PHP
Asked Answered
P

3

6

I've just read the following on the MailChimp website:

MailChimp API v3.0 is now live! Prior versions will no longer be supported after 2016, so all API users should begin transitioning to v3.0.

As a result, I would like to move to v3.0 of the API. Please could I have a function, in PHP, that returns a boolean, that will check whether an email address is subscribed to a specific MailChimp list. I do not want to subscribe that user, but merely check whether they are subscribed or not.

Poly answered 25/7, 2016 at 14:4 Comment(0)
S
9

UPDATE: I answered another question with a more elaborate tutorial of how to do this with jQuery .ajax(): Adding subscribers to a list using Mailchimp's API v3

Looking at the Mailchimp documentation and assuming you have a given list in mind, it looks like you would call this endpoint with a GET: /lists/{list_id}/members/{subscriber_hash}

To do this in PHP, I found a nice script sitting on github. Their last function would probably do the trick for you:

function mc_checklist($email, $debug, $apikey, $listid, $server) {
    $userid = md5($email);
    $auth = base64_encode( 'user:'. $apikey );
    $data = array(
        'apikey'        => $apikey,
        'email_address' => $email
        );
    $json_data = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.$listid.'/members/' . $userid);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
        'Authorization: Basic '. $auth));
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    $result = curl_exec($ch);
    if ($debug) {
        var_dump($result);
    }
    $json = json_decode($result);
    echo $json->{'status'};
}

If that function doesn't work, the only wrapper I could find for the v3 library works in conjunction with Laravel - Mailchimp v3 API PHP wrapper.

Sedgemoor answered 25/7, 2016 at 14:20 Comment(3)
Works perfectly except an extra dot is needed before api.mailchimp.com (or of course $server can have a dot at the end but that's illogical) - I've submitted an edit for this and removing die()Poly
I think Mailchimp uses the lower case version of a subscriber's email address to create the user ID. Thus, md5(strtolower($email)) might be a bit safer.Diacritical
This was working without fail, incorporated into a file via require, for a couple of years. Recently it stopped working, not returning "status" or other values. Using curl_error($ch) I was able to find an error of "HTTP/2 stream 1 was not closed cleanly: PROTOCOL_ERROR (err 1)." I see resolutions for this error with other APIs, but don't know what to do to resolve the issue with this. Any ideas?Reyreyes
L
13

If you use the mailchimp-api it looks like that

include 'Mailchimp.php';
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('your**api***key');

function emailExistsMc($subscriberMail, $list_id){
    global $MailChimp;
    $subscriber_hash = $MailChimp->subscriberHash($subscriberMail);
    $result = $MailChimp->get("lists/$list_id/members/$subscriber_hash");
    if($result['status'] == '404') return false;
    return true;
}

If $result['status'] is 404 then the resource was not found. Other possible values for $result['status'] are stated in the docs:

  • subscribed
  • unsubscribed
  • cleaned
  • pending
  • transactional
Lxx answered 4/5, 2017 at 9:2 Comment(0)
S
9

UPDATE: I answered another question with a more elaborate tutorial of how to do this with jQuery .ajax(): Adding subscribers to a list using Mailchimp's API v3

Looking at the Mailchimp documentation and assuming you have a given list in mind, it looks like you would call this endpoint with a GET: /lists/{list_id}/members/{subscriber_hash}

To do this in PHP, I found a nice script sitting on github. Their last function would probably do the trick for you:

function mc_checklist($email, $debug, $apikey, $listid, $server) {
    $userid = md5($email);
    $auth = base64_encode( 'user:'. $apikey );
    $data = array(
        'apikey'        => $apikey,
        'email_address' => $email
        );
    $json_data = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.$listid.'/members/' . $userid);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
        'Authorization: Basic '. $auth));
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    $result = curl_exec($ch);
    if ($debug) {
        var_dump($result);
    }
    $json = json_decode($result);
    echo $json->{'status'};
}

If that function doesn't work, the only wrapper I could find for the v3 library works in conjunction with Laravel - Mailchimp v3 API PHP wrapper.

Sedgemoor answered 25/7, 2016 at 14:20 Comment(3)
Works perfectly except an extra dot is needed before api.mailchimp.com (or of course $server can have a dot at the end but that's illogical) - I've submitted an edit for this and removing die()Poly
I think Mailchimp uses the lower case version of a subscriber's email address to create the user ID. Thus, md5(strtolower($email)) might be a bit safer.Diacritical
This was working without fail, incorporated into a file via require, for a couple of years. Recently it stopped working, not returning "status" or other values. Using curl_error($ch) I was able to find an error of "HTTP/2 stream 1 was not closed cleanly: PROTOCOL_ERROR (err 1)." I see resolutions for this error with other APIs, but don't know what to do to resolve the issue with this. Any ideas?Reyreyes
R
3

I use the DrewM library

function isSubscribed($emailAddress, $listId) {
    $chimp = new \DrewM\MailChimp\MailChimp($apiKeyHere);

    $subscriberHash = $chimp->subscriberHash($emailAddress);

    $result = $chimp->get('lists/' . $listId . '/members/' . $subscriberHash);

    return ($chimp->success() && isset($result['id']));
}
Rachelrachele answered 16/6, 2017 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.