I made the following `setup.php` several days ago. I run it by `php vendor/ems-api/php-client/example/setup.php`, it worked and returned 5356 blacklisted emails and 16484 unsubscribed emails. Alongside the folder `ems-api`, I have also a folder `twisted1919`, which is not used, I think.
Today, I would like to reproduce it inside a new empty folder `new`. I put the same file in `toRun/setup.php`, I did `composer require ems-api/php-client`, `mkdir -p toRun/data/cache` and `chmod -R 775 toRun/data/cache`. Then `php toRun/setup.php` returned a wired result: exactly 2000 blacklisted emails and exactly 1000 unsubscribed emails (it seems that the code stopped at the pagination).
Does anyone know why the setup in the new folder does not work well?
PHP:
<?php
require 'vendor/autoload.php';
// configuration object
$config = new \EmsApi\Config([
'apiUrl' => 'https://mail.mycompany.com/api',
'apiKey' => 'key',
// components
'components' => [
'cache' => [
'class' => \EmsApi\Cache\File::class,
'filesPath' => __DIR__ . '/data/cache', // make sure it is writable by webserver
]
],
]);
// now inject the configuration and we are ready to make api calls
\EmsApi\Base::setConfig($config);
// start UTC
date_default_timezone_set('UTC');
// CREATE THE ENDPOINT
$endpoint = new EmsApi\Endpoint\ListSubscribers();
// Define the list of statuses
$statuses = ['unapproved', 'disabled', 'moved', 'unconfirmed', 'blacklisted', 'unsubscribed', ];
// Define the list ID
$listId = 'wj872armkge75'; // "All the Contacts backup 20240105"
// Initialize an array to hold all the subscribers
$allSubscribers = [];
// Iterate over each status and fetch subscribers
foreach ($statuses as $status) {
$pageNumber = 1;
$perPage = 1000;
do {
$x = $pageNumber * $perPage;
echo "Processing status: $status, Page: $pageNumber, Number: $x\n";
// Fetch subscribers by status
$response = $endpoint->searchByStatus($listId, $status, $pageNumber, $perPage);
// Access the response data correctly
$responseData = $response->body->itemAt('data');
$responseStatus = $response->body->itemAt('status'); // correct
// Check if response is successful and has data
if ($responseStatus === 'success' && !empty($responseData['records'])) {
foreach ($responseData['records'] as $subscriber) {
$allSubscribers[] = [
'EMAIL' => $subscriber['EMAIL'],
'STATUS' => $status
];
}
$pageNumber++;
} else {
break;
}
} while (true);
}
// Path to save the CSV file
$csvFile = __DIR__ . '/emails_to_clean.csv';
// Open the file for writing
$handle = fopen($csvFile, 'w');
// Add CSV column headers
fputcsv($handle, ['EMAIL', 'STATUS']);
// Add the subscribers data
foreach ($allSubscribers as $subscriber) {
fputcsv($handle, $subscriber);
}
// Close the file
fclose($handle);
echo "Subscribers with specified statuses have been saved to the csv file.";
Today, I would like to reproduce it inside a new empty folder `new`. I put the same file in `toRun/setup.php`, I did `composer require ems-api/php-client`, `mkdir -p toRun/data/cache` and `chmod -R 775 toRun/data/cache`. Then `php toRun/setup.php` returned a wired result: exactly 2000 blacklisted emails and exactly 1000 unsubscribed emails (it seems that the code stopped at the pagination).
Does anyone know why the setup in the new folder does not work well?
Last edited: