geo data and other custom tags via api

tdw02d

New Member
Does anyone know how to pass geo data and other custom tags via api. When using mailwizz forms it works fine but when using an external script form the data does not show up via api.
 
PHP:
<?php

class Am_Newsletter_Plugin_Mailwizz extends Am_Newsletter_Plugin
{
    protected $initialized = false;

    public function __construct(Am_Di $di, array $config) {
        parent::__construct($di, $config);

        if (!$this->initialized) {
            if ($this->init_mailwizz($config)) {
                $this->initialized = true;
            }
        }
    }

    protected function init_mailwizz($config) {
        require_once dirname(__FILE__) . '/MailWizzApi/Autoloader.php';
        // register the autoloader.
        MailWizzApi_Autoloader::register();

        try {
            $mailconfig = new MailWizzApi_Config(array(
                'apiUrl'        => rtrim(preg_replace('|api/?$|i', '', $config['url']),'/') . '/api',
                'publicKey'     => $config['public_key'],
                'privateKey'    => $config['private_key'],

                'components' => array(
                    'cache' => array(
                        'class'     =>  'MailWizzApi_Cache_File',
                        'filesPath' =>  dirname(__FILE__) .
                                        '/MailWizzApi/Cache/data/cache'
                    )
                ),
            ));
            MailWizzApi_Base::setConfig($mailconfig);
        } catch(Exception $e) {
            $this->getDi()->errorLogTable->log($e->getMessage());
        }

        return true;
    }

    function _initSetupForm(Am_Form_Setup $form)
    {
        $el = $form->addText('public_key', array('class' => 'el-wide'))->setLabel("Mailwizz Public Key");
        $el->addRule(   'regex',
                        'API Keys must be in form of 40 hex lowercase signs',
                        '/^[a-f0-9]{40}$/');
        $el->addRule('required');

        $el = $form->addText('private_key', array('class' => 'el-wide'))->setLabel("Mailwizz Private Key");
        $el->addRule(   'regex',
                        'API Keys must be in form of 40 hex lowercase signs',
                        '/^[a-f0-9]{40}$/');
        $el->addRule('required');

        $el = $form->addText('url', array('class' => 'el-wide'))
            ->setLabel("Mailwizz URL");
        $el->addRule('callback', 'The URL isn\'t valid', function($url) {
            if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
                return false;
            }

            return true;
        });
        $el->addRule('required');
    }

    function isConfigured()
    {
        return $this->init_mailwizz($this->getConfig());
    }

    public function changeSubscription(User $user, array $addLists, array $deleteLists)
    {
        $endpoint = new MailWizzApi_Endpoint_ListSubscribers();
        foreach ($addLists as $list_id)
        {
            $response = $endpoint->createUpdate($list_id, array(
                'EMAIL'    => $user->email,
                'FNAME'    => $user->name_f,
                'AMUSER'   => $user->login
            ));

            if ($response->curlCode) {
                throw new Exception($response->curlMessage);
            }
        }

        foreach ($deleteLists as $list_id)
        {
            $response = $endpoint->deleteByEmail($list_id, $user->email);

            if ($response->curlCode) {
                throw new Exception($response->curlMessage);
            }
        }

        return true;
    }

    public function getLists()
    {
        $endpoint = new MailWizzApi_Endpoint_Lists();
        $ret = array();

        $response = $endpoint->getLists(1, 999999);

        if ($response->curlCode) {
            throw new Exception($response->curlMessage);
        }

        $response = $response->body->toArray();

        if (!empty($response['data']['records'])) {
            foreach ($response['data']['records'] as $el) {
                $ret[$el['general']['list_uid']] = array(
                    'title' => $el['general']['display_name'],
                );
            }
        }

        return $ret;
    }

    public function getReadme()
    {
        return <<<CUT
Mailwizz plugin readme

This module allows aMember Pro users to subscribe/unsubscribe from e-mail lists
created in Mailwizz. To configure the module:

 - Set Public, Private keys and url in the module config page
CUT;
    }
}
 
Last edited by a moderator:
Just realized that on the list subscribers page it will show geodata from the IP at the time of submission and when I click on the subscriber it shows geodata from the IP of the server that sent the API call... still don't know why the geodata is not showing on the list subscribers page for API submissions tho. Maybe I need to add subscriber IP to the API call script? Any ideas...
 
Maybe I need to add subscriber IP to the API call script?
Yes!
You can do this:
PHP:
$response = $endpoint->createUpdate($list_id, array(
                'EMAIL'    => $user->email,
                'FNAME'    => $user->name_f,
                'AMUSER'   => $user->login,
                'details' => [  'ip_address' => $_SERVER['REMOTE_ADDR'] ],
            ));
 
The IPs now match but the geodata is still missing from the list subscribers page...
Geodata shows on this page...
2018-01-04 11_10_27-MailWizz _ Update existing list subscriber..png

But not on this page...

2018-01-04 09_30_23-MailWizz _ Your mail list subscribers.png
 
Last edited:
Back
Top