Get Subcribe Email List From Mailchimp Using Php Programmatically

Here we can get subscribed email list  from  Mailchimp using programmatically PHP
This method is straightforward and simple anyone can use this code and implement it into core PHP, all PHP frameworks like WordPress, laravel, etc
we get a simple list that does not use an extra class and is not required to include any file or function.

Step 1:
we need to API key and listing id Mailchimp provides the API key and listing id following image help for find out it

Step 2:
Implement the following code into your PHP file and replace your API key and listing id.

<?php
$api_key = 'api_key';
$list_id = 'list_id';

$dc = substr($api_key,strpos($api_key,'-')+1); // us5, us8 etc

// URL to connect
$url = 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id;

// connect and get results
$body = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key ) );

// number of members in this list
$member_count = $body->stats->member_count;
$emails = array();

for( $offset = 0; $offset < $member_count; $offset += 50 ) :
 
  $data = array(
    'offset' => $offset,
    'count'  => 50
  );

  // URL to connect
  $url = 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members';

  // connect and get results
  $body = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key, $data ) );

 	foreach ( $body->members as $member ) {
    $emails[] = $member->email_address;
  }

endfor;
 echo "<pre>";
print_r( $emails );
echo "</pre>";


function rudr_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);
}
?>

Step 3:
Check and review results.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories