PHP code example of getanewsletter / api-php

1. Go to this page and download the library: Download getanewsletter/api-php 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/ */

    

getanewsletter / api-php example snippets




$token = '...';
$gan = new \Gan\Api($token);



$contactManager = new \Gan\ContactManager($gan);
$contact = $contactManager->get('[email protected]');



try {
    $contact = $contactManager->get('[email protected]');
} catch (\Gan\ApiException $e) {
    if ($e->response->code === 404) {
        echo 'Contact not found!';
    } else {
        echo 'API error: ' . $e;
    }
}



$contact = new \Gan\Contact();
$contact->email = '[email protected]';
$contact->first_name = 'Jane';

$contactManager->save($contact);



$contactManager->overwrite($contact);



$contact = $contactManager->save($contact);
echo $contact->created;



// Get the contact.
$contact = $contactManager->get('[email protected]');
// Change some fields.
$contact->first_name = 'John';
// Save it.
$contactManager->save($contact);



$contact = new \Gan\Contact();
$contact->setPersisted();
$contact->email = '[email protected]';
$contact->first_name = 'John';
$contactManager->save($contact);



$contactManager->delete($contact);



$listManager = new \Gan\NewsletterManager($gan);

// Retrieve a list.
$list = $listManager->get('hash');

// Update the list.
$list->name = 'my list';
$list = $listManager->save($list);
echo $list->updated;

// Create new list.
$new_list = new \Gan\Newsletter();
$new_list->email = '[email protected]'; // requred fields
$new_list->name = 'my new list';
$new_list->sender = 'John Doe';
$listManager->save($new_list);

// Partial update.
$list = new \Gan\Newsletter();
$list->hash = 'hash'; // lookup field
$list->name = 'updated list';
$listManager->save($list);

// Delete the list.
$listManager->delete($list);




$contact->subscribeTo($list);
$contactManager->save($contact);



$contact = new \Gan\Contact();
$contact->email = '[email protected]';
$contact->subscribeTo($list);
$contactManager->save($contact);
$token