PHP code example of nztim / mailchimp

1. Go to this page and download the library: Download nztim/mailchimp 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/ */

    

nztim / mailchimp example snippets


// Get an array of all available lists:
Mailchimp::getLists();

// Get lists with parameters - get IDs of lists a user is subscribed to:
Mailchimp::getLists(['email' => '[email protected]', 'fields' => 'lists.id']);

// Check to see if an email address is subscribed to a list:
Mailchimp::check($listId, $emailAddress); // Returns boolean

// Check the staus of a subscriber:
Mailchimp::status($listId, $emailAddress); // Returns 'subscribed', 'unsubscribed', 'cleaned', 'pending', 'transactional' or 'not found'

// Get subscriber tags:
Mailchimp::getTags($listId, $emailAddress); // Returns array of Tag objects

// Add tags to a subscriber
Mailchimp::addTags($listId, $emailAddress, $tags); // $tags = ['tag1', 'tag2']

// Remove tags from a subscriber
Mailchimp::removeTags($listId, $emailAddress, $tags); // $tags = ['tag1', 'tag2']

// Remove all tags from a subscriber
Mailchimp::removeAllTags($listId, $emailAddress); 

// Adds/updates an existing subscriber:
Mailchimp::subscribe($listId, $emailAddress, $merge = [], $confirm = true);
// Use $confirm = false to skip double-opt-in if you already have permission.
// This method will update an existing subscriber and will not ask an existing subscriber to re-confirm.

// Unsubscribe a member (set status to 'unsubscribed'):
Mailchimp::unsubscribe($listId, $emailAddress);

// Archive a member (no longer counts towards audience limits):
Mailchimp::archive($listId, $emailAddress);

// Permanently delete a member record:
Mailchimp::delete($listId, $emailAddress);
// Use with care: deleted members cannot be re-added without the user subscribing via a Mailchimp-hosted form with double-opt-in confirmation.

// Directly call the API:
Mailchimp::api($method, $endpoint, $data = []); // Returns an array.

$member = (new NZTim\Mailchimp\Member($email))
    ->merge_fields(['FNAME' => 'First name'])
    ->email_type('text')
    ->confirm(false);
Mailchimp::addUpdateMember($listId, $member);

// Laravel:
// Subscribe a user to your list, existing subscribers will not receive confirmation emails
Mailchimp::subscribe('listid', '[email protected]');

// Subscribe a user to your list with merge fields and double-opt-in confirmation disabled
Mailchimp::subscribe('listid', '[email protected]', ['FNAME' => 'First name', 'LNAME' => 'Last name'], false);

// Subscribe/update a user using the Member class
$member = (new NZTim\Mailchimp\Member($email))->interests(['abc123fed' => true])->language('th');
Mailchimp::addUpdateMember('listid', $member);