Skip to content

Instantly share code, notes, and snippets.

@chetansatasiya
Created June 5, 2017 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chetansatasiya/3ae9d2152487490c734bcf1884ec9fe1 to your computer and use it in GitHub Desktop.
Save chetansatasiya/3ae9d2152487490c734bcf1884ec9fe1 to your computer and use it in GitHub Desktop.
MailChimp API 3.0 and WordPress HTTP API
<?php
// Enter MailChimp API key and List Id.
$api_key = 'YOUR API KEY';
$list_id = 'YOUR LIST ID';
$dc = substr($api_key,strpos($api_key,'-')+1); // datacenter, it is the part of your api key - us5, us8 etc
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
)
);
$response = wp_remote_get( 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id, $args );
$body = json_decode( $response['body'] );
if ( $response['response']['code'] == 200 ) {
echo $body->stats->member_count;
} else {
echo '<b>' . $response['response']['code'] . $body->title . ':</b> ' . $body->detail;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment