Top Questions

  • 5.11 Years

    • Category : Php
    • Location : Mohali
    • Posted On : Aug 17, 2017

    MailChimp REST Api V3 exposes methods that allows you to add, change subscribed to unsubscribed, delete list member and get all member list of a specific MailChimp list.

    The following code snippet help you to add a new subscriber to MailChimp List.
    <?php
    $api_key = 'API_KEY';
    $list_id = 'LIST_ID';
    
    $dataCenter = substr($api_key,strpos($api_key,'-') + 1);
    $url = 'https://'.$dataCenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members';
       $pfb_data = array(
        'email_address' => "YOUR_EMAIL_ID",
        'status'        => 'subscribed',
        'merge_fields'  => array(
          'FNAME'       => "YOUR_FIRST_NAME",
          'LNAME'       => "YOUR_LAST_NAME"
        ),
      );
    
    // connect and get results
    $result = json_decode( dorcode_mailchimp_curl_connect( $url, 'POST',$api_key, $pfb_data ) );
    
    if( $result->status == 400){
    	// error
       echo "already subscriber.";
    } elseif( $result->status == 'subscribed') {
    	// success
    	echo "subscribered";
    }
    ?>
    MailChimp change subscribed to unsubscribed.
    <?php
    $api_key = 'API_KEY';
    $list_id = 'LIST_ID';
    $email_to_unsubscribe = 'YOUR_EMAIL_ID';
    
    $dataCenter = substr($api_key,strpos($api_key,'-')+ 1);
    $url = 'https://'.$dataCenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/'.md5($email_to_unsubscribe);
    $result = json_decode(dorcode_mailchimp_curl_connect($url, 'PUT',$api_key, array('status'=>'unsubscribed')));
    if( $result->status == 400){
    	// error
        echo $result->errors[0]->message;
    } elseif( $result->status == 'unsubscribed') {
    	// success
        echo "unsubscribed";
    }
    ?>
    MailChimp delete list member.
    <?php
    $api_key = 'API_KEY';
    $list_id = 'LIST_ID';
    $email_to_unsubscribe = 'YOUR_EMAIL_ID';
    
    $dataCenter = substr($api_key,strpos($api_key,'-')+ 1);
    $url = 'https://'.$dataCenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/'.md5($email_to_unsubscribe);
    $result = json_decode(dorcode_mailchimp_curl_connect($url, 'DELETE',$api_key, array('status'=>'DELETE')));
    echo $result; // output "null"
    ?>
    MailChimp get all member list.
    <?php
    $api_key = 'API_KEY';
    $list_id = 'LIST_ID';
    
    $dataCenter = substr($api_key,strpos($api_key,'-')+ 1);
    $url = 'https://'.$dataCenter.'.api.mailchimp.com/3.0/lists/'.$list_id;
    $body = json_decode(dorcode_mailchimp_curl_connect($url,'GET',$api_key));
    
    // number of members in this list
    $member_count = $body->stats->member_count;
    $email = array();
     
    for( $offset = 0; $offset < ($member_count+50); $offset  = $offset+50 ) :
        $data = array(
            'offset' => $offset,
            'count'  => 50
        );
        // URL to connect
        $url = 'https://'.$dataCenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members';
        // connect and get results
        $body = json_decode(dorcode_mailchimp_curl_connect($url, 'GET',$api_key, $data));
        foreach ( $body->members as $member ) {
        	// if($member->status=="subscribed") if you want to get members where status is subscribed
              $email[] = $member->email_address;
        }
    endfor;
    print_r($email);
    ?>
    MailChimp curl main php function.
    <?php
    function dorcode_mailchimp_curl_connect($url, $request_type, $api_key, $data = array()) {
    
    
        if( $request_type == 'GET' )
            $url .= '?' . http_build_query($data);
     
        $mch = curl_init();
        $headers = array(
            'Content-Type: application/json',
            'Authorization: Basic '.base64_encode( 'user:'. $api_key )
        );
        curl_setopt($mch, CURLOPT_URL, $url );
        curl_setopt($mch, CURLOPT_HTTPHEADER, $headers);
        //curl_setopt($mch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
        curl_setopt($mch, CURLOPT_RETURNTRANSFER, true); // do not echo the result, write it into variable
        curl_setopt($mch, CURLOPT_CUSTOMREQUEST, $request_type); // according to MailChimp API: POST/GET/PATCH/PUT/DELETE
        curl_setopt($mch, CURLOPT_TIMEOUT, 10);
        curl_setopt($mch, CURLOPT_SSL_VERIFYPEER, false); // certificate verification for TLS/SSL connection
     
        if( $request_type != 'GET' ) {
            curl_setopt($mch, CURLOPT_POST, true);
            curl_setopt($mch, CURLOPT_POSTFIELDS, json_encode($data)); // send data in json
        }
     
        return curl_exec($mch);
    }
    
    ?>
     

Add Your Answer

When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.