Reputation: 171
I'm dealing with a problem retrieving subscribers from a Mailchimp list. I am using the official PHP Mailchimp Marketing API library and Codeigniter.
Here's my code:
private function getMailchimpListSubscribers(string $list, int $count, int $offset)
{
$response = $this->mailchimpMarketing
->lists
->getListMembersInfo(
$list,
null,
null,
$count,
$offset,
null,
'subscribed'
);
if ($response) {
log_message('error', $response->total_items);
return $response;
}
}
private function getProductionSubscribers(string $list = 'test'): array
{
$finalTos = [];
$count = 1000;
$offset = 0;
do {
$tos = $this->getMailchimpListSubscribers($list, $count, $offset);
if (!empty($tos->members)) {
$finalTos = array_merge($finalTos, $tos->members);
}
$offset = $offset + $count;
log_message('error', $offset);
} while ($offset < $tos->total_items);
$callback = fn($member) => [
"email" => $member->email_address,
"type" => "to"
];
return array_map($callback, $finalTos);
}
Using the logging, I can see that the offset and the total items are working as expected. Offset is increasing until the max number of members on the list.
ERROR - 2023-07-18 08:53:59 --> 2651 <--total_members
ERROR - 2023-07-18 08:53:59 --> 1000 <-- offset
ERROR - 2023-07-18 08:54:17 --> 2651
ERROR - 2023-07-18 08:54:17 --> 2000
ERROR - 2023-07-18 08:54:32 --> 2651
ERROR - 2023-07-18 08:54:32 --> 3000
However, I always get 1001 items but not the full list.
Upvotes: 2
Views: 141