Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon can you provide a code snippet for updating subscriber information in Mailchimp API
    Mailchimp API PHP code update subscriber archive member Mailchimp API fields merge fields subscriber status


can you provide a code snippet for updating subscriber information in Mailchimp API


Here is a code snippet in PHP that updates subscriber information in Mailchimp using the API:

php
<?php
require_once 'path/to/mailchimp-api/vendor/autoload.php';

use \DrewM\MailChimp\MailChimp;

// Initialize the Mailchimp API client
$mailchimp = new MailChimp(MAILCHIMP_API_KEY);
$mailchimp->setConfig(['server' => 'YOUR_SERVER_PREFIX']);

// Get the list ID
$listId = '**********';

// Get the subscriber ID
$subscriberId = 'md5:
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy8918 = 's&#117;bscr&#105;b&#101;r' + '&#64;';
 addy8918 = addy8918 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy8918 + suffix + '\'' + attribs + '>' );
 document.write( addy8918 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>';

// Update subscriber information
$mailchimp->patch("lists/$listId/members/$subscriberId", [
    'email_address' => '
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy19938 = 's&#117;bscr&#105;b&#101;r' + '&#64;';
 addy19938 = addy19938 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy19938 + suffix + '\'' + attribs + '>' );
 document.write( addy19938 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>',
    'status' => 'subscribed',
    'merge_fields' => [
        'FNAME' => 'First Name',
        'LNAME' => 'Last Name',
    ],
]);

?>

This code snippet updates the subscriber information for the subscriber with the specified ID. It sets the email address, status, and merge fields (first name and last name) for the subscriber.

Parameters:

- `$listId`: The ID of the Mailchimp list.
- `$subscriberId`: The ID of the subscriber to update.
- `$emailAddress`: The new email address for the subscriber.
- `$status`: The new status for the subscriber (e.g., "subscribed", "unsubscribed", etc.).
- `$mergeFields`: An array of merge fields to update for the subscriber.

Example Output:

json
{
    "status": "success",
    "msg": "OK",
    "result": {
        "email_address": "
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy93929 = 's&#117;bscr&#105;b&#101;r' + '&#64;';
 addy93929 = addy93929 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy93929 + suffix + '\'' + attribs + '>' );
 document.write( addy93929 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>",
        "status": "subscribed",
        "merge_fields": {
            "FNAME": "First Name",
            "LNAME": "Last Name"
        }
    }
}

This code snippet demonstrates how to update subscriber information in Mailchimp using the API.

what is the syntax for archiving a list member in Mailchimp via the API

The syntax for archiving a list member in Mailchimp via the API is:

bash
DELETE /lists/{list_id}/members/{subscriber_hash}

Replace `{list_id}` with the ID of the list you want to archive the member from and `{subscriber_hash}` with the MD5 hash of the lowercase version of the list member's email address.

For example:

bash
DELETE /lists/12345678/members/md5:
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy68383 = 's&#117;bscr&#105;b&#101;r' + '&#64;';
 addy68383 = addy68383 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy68383 + suffix + '\'' + attribs + '>' );
 document.write( addy68383 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>

This will archive the list member with the email address `
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
` from the list with the ID `12345678`.

Citations:
[1] https://stackoverflow.com/questions/63584277/how-can-i-unarchive-a-member-using-the-mailchimp-api
[2] https://mailchimp.com/developer/marketing/api/list-members/
[3] https://mailchimp.com/developer/marketing/api/lists/add-list/
[4] https://mailchimp.com/developer/marketing/api/lists/get-list-info/
[5] https://github.com/Gemini-Commerce/mailchimp-marketing-go/blob/master/docs/ListsApi.md

what are the common fields to update for a subscriber in Mailchimp API

The common fields to update for a subscriber in Mailchimp API include:

1. Email Address: Update the subscriber's email address using the `email_address` field.
2. Status: Update the subscriber's status using the `status` field. This can be set to "subscribed", "unsubscribed", "cleaned", or "bounced".
3. Merge Fields: Update the subscriber's merge fields using the `merge_fields` field. This includes fields like `FNAME`, `LNAME`, `PHONE`, `ADDRESS`, etc.
4. Interests: Update the subscriber's interests using the `interests` field. This includes the categories and names of the interests.
5. Location: Update the subscriber's location using the `location` field. This includes the country, region, and city.
6. Profile: Update the subscriber's profile using the `profile` field. This includes fields like `FIRST_NAME`, `LAST_NAME`, `PHONE`, `ADDRESS`, etc.
7. Tags: Update the subscriber's tags using the `tags` field. This includes custom tags that you can create for your subscribers.

Here is an example of how to update a subscriber's information using the Mailchimp API:

bash
PATCH /lists/{list_id}/members/{subscriber_hash}

json
{
    "email_address": "
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy81779 = 'n&#101;w_&#101;m&#97;&#105;l' + '&#64;';
 addy81779 = addy81779 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy81779 + suffix + '\'' + attribs + '>' );
 document.write( addy81779 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>",
    "status": "subscribed",
    "merge_fields": {
        "FNAME": "New First Name",
        "LNAME": "New Last Name"
    },
    "interests": [
        {
            "category_id": "12345678",
            "name": "New Interest"
        }
    ]
}

This example updates the subscriber's email address, status, merge fields, and interests.

Citations:
[1] https://mailchimp.com/help/manage-audience-signup-form-fields/
[2] https://mailchimp.com/developer/marketing/api/list-members/
[3] https://mailchimp.com/developer/marketing/api/lists/batch-subscribe-or-unsubscribe/
[4] https://mailchimp.com/developer/marketing/api/lists/add-list/
[5] https://mailchimp.com/developer/marketing/api/lists/get-list-info/