Setup ems-api

SoftTimur

Member
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.

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:
Hello,
The location from where you are running the script has nothing to do with the fetched results. For sure there is some logic issue in your script, or maybe things changed in your application.

Cosmin
 
Hello,
The location from where you are running the script has nothing to do with the fetched results. For sure there is some logic issue in your script, or maybe things changed in your application.

Cosmin
I realize that even with the previous location, results are not always correct. Results can be good for list A, while they can be bad for list B (The API call failed at certain pages).

Do you have any idea why it's happening?
 
Last edited:
Hello,
The location from where you are running the script has nothing to do with the fetched results. For sure there is some logic issue in your script, or maybe things changed in your application.

Cosmin
I'm able to print the message of the failed API call on certain pages. Do you know e.g., where we can increase the timeout limit?


(
[_data:EmsApi\Params:private] => Array
(
[status] => error
[error] => Operation timed out after 30002 milliseconds with 228856 bytes received
)
[_readOnly:EmsApi\Params:private] =>
)
 
Do you have any idea why it's happening?
Not really. From your code I can see that you stopping at the first error response from the API. That's might be the reason. Also might be related to the above timeout. Try to request lower batches like 100.

Cosmin
 
Not really. From your code I can see that you stopping at the first error response from the API. That's might be the reason. Also might be related to the above timeout. Try to request lower batches like 100.

Cosmin
I set `$timeout = 60` in vendor/ems-api/php-client/src/Http/Client.php and `$perPage = 500`, it worked. Thank you.
 
It is not a good idea to modify the vendor files. The 60 seconds timeout is enough and per page can be set from your script.
 
Back
Top