PHP code example of richardhj / newsletter2go-api

1. Go to this page and download the library: Download richardhj/newsletter2go-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

richardhj / newsletter2go-api example snippets


$users = Richardhj\Newsletter2Go\Api\Model\NewsletterUser::findAll(null, $apiCredentials);

// Use a PHPDoc comment and profit from auto suggestion
/** @var NewsletterUser $user */
foreach ($users as $user) {
    // What's about naming all users "Doe"?
    $user->setLastName('Doe');
    // Save the user (via the API of course)
    $user->save();
    
    // $data contains all data fetched for this item
    $data = $user->getData();
}

$recipients = Richardhj\Newsletter2Go\Api\Model\NewsletterRecipient::findByListAndGroup('abc123', 'xyz987', null, $apiCredentials);
var_dump($recipients);

foreach ($recipients as $recipient) {
    $recipient->addToGroup('xyz345');
    $recipient->removeFromGroup('asdf12');
}

// Use the ApiCredentialsFactory
$apiCredentials = ApiCredentialsFactory::createFromUsernameAndPassword('secret_auth_token', '[email protected]', 'open_sesame');
$apiCredentials = ApiCredentialsFactory::createFromRefreshToken('secret_auth_token', 'secret_users_refresh_token');
// Or simply use ::create()
$apiCredentials = ApiCredentialsFactory::create('secret_auth_token', 'secret_users_refresh_token');

$getParams = new Richardhj\Newsletter2Go\Api\Tool\GetParameters();
$getParams
    ->setExpand(true)
    ->setFilter('email=like="%@example.org"')
    ->setOffset(2)
    ->setLimit(1);

$recipients = Richardhj\Newsletter2Go\Api\Model\NewsletterRecipient::findByListAndGroup('abc123', 'xyz987', $getParams, $apiCredentials);
var_dump($recipients);

$recipient = new Richardhj\Newsletter2Go\Api\Model\NewsletterRecipient();
$recipient->setApiCredentials($apiCredentials);
$recipient
    ->setListId('abc123')
    ->setFirstName('John')
    ->setLastName('Doe')
    ->setEmail('[email protected]')
    ->setGender('m');

// Good to have an id, otherwise the email address will be the primary key and you will not be able to change the email address of a recipient properly
$recipient->setId('xyz123');

// Update an existing recipient (when id given or email address known in Newsletter2Go) or create a new recipient
$recipient->save();

$groups = Newsletter2Go\Api\Model\NewsletterGroup::findByList('abc123', $getParams, $credentials);

/** @var NewsletterGroup $group */
foreach ($groups as $group) {
    $group->delete();
}