Monday 2 January 2017

Mailchimp api 3.0 check if email is subscribed Solution

Hello guys is me again with another fresh tutorial on mailchimp api 3.0 this tutorial will help you to verify existing email on a mail chimp list, it will check if an email is subscribed on a particular list on your mail chimp account. Here is the solution, if you ever wondered why. Alright follow the steps and you do just fine or download to try it out yourself.

Demo Download

First get your Mail Chimp API credentials by signing into your mail chimp account



So, now click on your profile click account

Follow the screenshot if you are lost or comment below, select extra, click on API Keys
Click create a key to auto generate new API key, we are going to use it later.

Now the next is to get the list ID were users are subscribed to, select your favorite list.

Now soon as you click arrow a menu will display select SETTINGS.
There you find the list ID, Note will are going to use it later on.
First we are going to have two files let called them
  • Index.php
This php file contain subscriber form and validating form for existing subscribers and new subscibers.
  • Action.php
Action page contain php scipt for validating the subscriber if user is subscribed or not.

Index.php

<?php
session_start();
$statusMsg = !empty($_SESSION['msg'])?$_SESSION['msg']:'';
unset($_SESSION['msg']);
?>
<!DOCTYPE HTML>
<html>
<head>
    <title>Add subscriber to list using MailChimp API and PHP by CodexWorld</title>
</head>
<body>
 <h2>Subscribe Our Newsletter</h2>
    <?php echo $statusMsg; ?>
    <form method="post" action="action1.php">
    
        <p><label>Email: </label><input type="text" name="email" /></p>
        <p><input type="submit" name="submit" value="SUBSCRIBE"/></p>
    </form>
</body>
</html>



action.php

<?php
session_start();
if(isset($_POST['submit'])){

/* Database connection start */


    $email = $_POST['email'];
    $file_id = $_POST['file'];
    if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
        // MailChimp API credentials
        $apiKey = 'YOUR_API_KEY';
        $listID = 'YOUR_LIST_ID';
        
        // MailChimp API URL
        $memberID = md5(strtolower($email));
        $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
        $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID;

        // member information
$arr = array('email_address' => $email, 'status' => '');
$json = json_encode($arr);

        
        // send a HTTP POST request with curl
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        $result = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
     
        $json = json_decode($result);

        // store the status message based on response code
        if ($json->{'status'} == 400) {

                $_SESSION['msg'] = '
                <!-- Begin MailChimp Signup Form -->
<link href="//cdn-images.mailchimp.com/embedcode/classic-10_7.css" rel="stylesheet" type="text/css">
<style type="text/css">
    #mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }
    /* Add your own MailChimp form style overrides in your site stylesheet or in this style block.
       We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
</style>

<div id="mc_embed_signup">
<form action="//codexpresslabs.us13.list-manage.com/subscribe/post?u=065a5a8d381de6c1aaba29e8c&amp;id=e5692a9974" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>

    <div id="mc_embed_signup_scroll">
    <div class="formstatus error">You have not subscribe to our mail List!</div>
    <h2>Subscribe to our mailing list</h2>
<div class="indicates-required"><span class="asterisk">*</span> indicates required</div>
<div class="mc-field-group">
    <label for="mce-EMAIL">Email Address  <span class="asterisk">*</span>
</label>
    <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>

    <div id="mce-responses" class="clear">
        <div class="response" id="mce-error-response" style="display:none"></div>
        <div class="response" id="mce-success-response" style="display:none"></div>
    </div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_065a5a8d381de6c1aaba29e8c_e5692a9974" tabindex="-1" value=""></div>
    <div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
    </div>
</form>
</div>
<!--End mc_embed_signup-->
';  

        } elseif($json->{'status'} == 'subscribed') {
            if ($num) {
               $_SESSION['msg'] = '<div class="formstatus" style="color: #EA4335"><a href="'.$url.'">Download Now</a></div>';
            }
            else
            {
               $_SESSION['msg'] = '<div class="formstatus error">This file does not exist!</div>';     
            }
            
        }
    }else{
        $_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
    }
}
// redirect to homepage
header('location:download.php?file='.$file_id.' ');  

Demo Download

Hopefully you will find it helpful...share like and subscibe
Previous Post
Next Post

post written by:

1 comment:

  1. EverytҺing is very open with ɑ really cleaг description of the issues.
    It wаs гeally informative. Yօur site is useful. Thanks for shaгing!

    ReplyDelete

Hit me with a comment!