PHP code example of chimpmatic / mailchimp-audience-finder

1. Go to this page and download the library: Download chimpmatic/mailchimp-audience-finder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chimpmatic / mailchimp-audience-finder example snippets


use Chimpmatic\AudienceFinder\AudienceFinder;

$finder = new AudienceFinder('your-api-key-us4');

// Get all audiences
$audiences = $finder->findAll();

foreach ($audiences as $audience) {
    echo $audience->id;          // "abc123def4"
    echo $audience->name;        // "Newsletter Subscribers"
    echo $audience->memberCount; // 1500
    echo $audience->doubleOptIn; // true

    // Merge fields included automatically
    foreach ($audience->mergeFields as $field) {
        echo $field->tag;         // "FNAME"
        echo $field->name;        // "First Name"
        echo $field->type;        // "text"
        echo $field->

$audience = $finder->findById('abc123def4');

echo $audience->getSummary();
// "Newsletter Subscribers (ID: abc123def4) — 1500 members, 4 merge fields, double opt-in: yes"

$audience = $finder->findByName('newsletter');
// Case-insensitive partial match — returns first match or null

$audience = $finder->findById('abc123def4');

// Find a specific field
$fname = $audience->getMergeFieldByTag('FNAME');
echo $fname->name;         // "First Name"
echo $fname->getMergeTag(); // "*|FNAME|*"

// Get all 

$fields = $finder->getMergeFields('abc123def4');

foreach ($fields as $field) {
    echo sprintf('%s (%s) — %s', $field->tag, $field->type, $field->name);
    // "FNAME (text) — First Name"
    // "BIRTHDAY (birthday) — Birthday"
}

use Chimpmatic\AudienceFinder\ApiException;

try {
    $finder = new AudienceFinder('invalid-key');
    $finder->findAll();
} catch (\InvalidArgumentException $e) {
    // Bad API key format
} catch (ApiException $e) {
    // API error (401, 404, network failure)
}