MailWizzApi usage inside namespace...

NAWHQ

New Member
Hi,
a few days ago I started my adventure with MailWizz so I don't know the MailWizz API very well but I'm learning. I want to use the API to subscribe users automatically using code call from our existing registration system (based on Concrete5 CMS). I created a dry code based on the examples provided with the MailWizz API, I put MailWizz on the server and everything works fine. I am able to subscribe users to lists from my clean, external PHP code without any problems using the provided API & examples on the target server and domain. So everything works fine.

The problem arises as soon as I try to close that code in any namespace, and unfortunately the place where I want to attach this code just forcing me to put it in the CMS namespace by default. When I put code in a namespace then the API stops working. It is enough to add any namespace in the example code file and it cuts it off completely from access to API functions.

I've never used namespaces more extensively in PHP programming, just following the documentation requirements when needed and never with problems. So maybe I omit something unknowingly and the solution is very simple? Is there any way to put the code from the examples in a namespace? Alternatively, how to force PHP to ignore the namespaces of the CMS at the moment when the code is including MailWizz API? Even MailWizz API simplest example code? I don't need anything else to move on. Just run any MailWizz API example in any random php namespace other than none.

Thanks a lot for any suggestions and help. :)

Kajetan @ NawHQ
 

twisted1919

Administrator
Staff member
@NAWHQ - MailWizz's api is in the global namespace, so if you are inside a namespace, use mailwizz's classes with a forward slash in front of the class name, i.e: instead of new MailWizzApi_Config(), use new \MailWizzApi_Config()
 

NAWHQ

New Member
Thanks for answer,
the external code I wrote using namespaces started working very well. Thank you.

The problem, however, remaining when I use it inside Concrete5 CMS. I get the error "Call to undefined method MailWizzApi_Config::get()". this is strange because I never call a function get().This call must be inside mailwizz api code.I don't want to modify the internal mailwizz code.

For experiments, I use a very simple code:
PHP:
require_once '/path/to/MailWizzApi/Autoloader.php';
\MailWizzApi_Autoloader::register();

$config = new \MailWizzApi_Config(array(
    'apiUrl'        => 'https://xxxxx',
    'publicKey'     => 'xxxxx',
    'privateKey'    => 'xxxxx',

    // components
    'components' => array(
        'cache' => array(
            'class'     => 'MailWizzApi_Cache_File',
            'filesPath' => '/path/to/MailWizzApi/Cache/data/cache', // it is writable
        )
    ),
));

\MailWizzApi_Base::setConfig($config);

$endpointListSubscribers = new \MailWizzApi_Endpoint_ListSubscribers();
// ADD SUBSCRIBER
$response = $endpointListSubscribers->create('xxxxx', array(
    'EMAIL'    => 'xxxx@xxxx.com',
    'FNAME'    => 'John',
    'LNAME'    => 'Doe'
));
Works very well on all my scripts, after adding slash with namespacs too. But it immediately stops working as soon as I add it to the Concrete5 code.

I thought it was a namespace issue. But as we can see, adding these slashes doesn't help. The paths are entered correctly. I using paths from the server root to be sure. I don't use relative paths. Any ideas of what else could be causing the error "Call to undefined method MailWizzApi_Config::get()"which I never use in my code?

Thank you.
 

NAWHQ

New Member
I found a solution.

The problem was that in the global namespace, the authors of Concrete5 kept the same variable names as they were in the MailWizz API code samples. For example $config. It was enough to change these variable names to anything with a prefix - for example $mwAPI_xxx. Concrete5 has its own error display system and it was not showing to me exactly at what point in the code this error appeared. It wasn't clear until I walked around it (by turning on code error preview, disabled by default). Then I noticed that this call was made in the concrete5 code and I realized that the variables were overwritten. Concrete5 was calling instructions somewhere in its code, for example: $config::get() which was not in the config variable anymore because it was overwritten previously by my insertion of the declaration $config code with the MailWizz API.

Summarizing here are the solutions to two problems:
1. in namespaces you have to add '\' before functions to get to them. Thanks for that sugestion.
2. as we are coding in the global namespace WE MUST REMEMBER THIS IS GLOBAL. It makes sense to use some safe prefixes in variable names especially when writing integration with other systems. $mwAPI_XXX for example :)

My little suggestion: maybe its worth to use such prefixes in your documentation examples, mention about namespaces and make users aware of these issues? The solution was so trivial but easy to miss by not having access to complete error informationand and such extra stuff in the documentation could save a lot of time for some people, especially those who are starting their adventure with your API.

Thanks again for your help. Now I can move on with code :)
 
Last edited:
Top