Mailerlite PHP API - Unassign Subscriber From Group

20 views Asked by At

I am using the Mailerlite PHP API in a Laravel app - mailerlite/mailerlite-api-v2-php-sdk - and having problems unasigning a subscriber from a group. I can create a new subscriber like this:

$this->mailerLite = new \MailerLiteApi\MailerLite(env('MAILERLITE_API_KEY'));

...

$subscribersApi = $this->mailerLite->subscribers();
$newSubscriber = $subscribersApi->create([
    'email' => trim($request['email']),
    'fields' => [
        'name' => trim($request['firstName']),
        'last_name' => trim($request['secondName'])
    ],
    'groups' => [
        'firstgroupID'
    ]
]);
Log::info('newSubscriber: ' . json_encode($newSubscriber));

And I can update the subscriber with a new groupID like this:

$subscribersApi = $this->mailerLite->subscribers();
$updateSubscriber = $subscribersApi->create([
    'email' => $user->email,
    'fields' => [
        'name' => $user->forename,
        'last_name' => $user->surname
    ],
    'groups' => [
        'newGroupID'
    ]
]);
Log::info('updateSubscriber: ' . json_encode($updateSubscriber));

But assigning the new group ID using the "create" method does not remove the original group ID as the method is non-destructive. The API documentation is pretty vague as to how to unasign a subscriber from a group via the PHP API. I have tried this:

$subscriberId = $updateSubscriber->id;
$groupsApi = $this->mailerLite->groups();
$unassignSubscriber = $groupsApi->unAssignSubscriber('GROUP-ID-TO-BE-REMOVED',$subscriberId);
Log::info('unassignSubscriber: ' . json_encode($unassignSubscriber));

But this generates the error:

local.ERROR: Error: Call to undefined method MailerLiteApi\Api\Groups::unAssignSubscriber() in /app/app/Http/Controllers/MailerliteController.php:66

Because this is a guess I made based on a different version of the MailerLite API. Can somebody please tell me how I can achieve the removal of a group from a subscriber in the MailerLte PHP API V2?

1

There are 1 answers

0
N.Widds On

So the answer in case anybody else is stuck is the following method:

removeSubscriber

And the relevant code is:

$subscriberId = $updateSubscriber->id;
$groupsApi = $this->mailerLite->groups();
$removeSubscriber = $groupsApi->removeSubscriber('GROUP-ID-TO-BE-REMOVED', $subscriberId);

It was easy to find in the codebase of the API on GitHub. Doh!