Mini Tutorial: Interacting with the Listrak API in Magento

October 2, 2012 by Corey

Listrak is a popular email marketing platform enabling websites collect user information and create marketing campaigns catered to them. They have recently released their new version (5.0) with a ton of new improvements which has made my coworkers and I very excited.

Here at Demac Media we’re beginning email campaigns with Listrak on a bunch of sites we maintain. They have a free Magento module entitled ‘Remarketing’ which syncs Magento information with your Listrak account, and after going through the module I can safely say it’s really well written. They even adhered to some of the sometimes cumbersome methods of writing a Magento module which is very impressive. I have been given the duty to code a module which uses their API functionality to give even further functionality to their ‘Remarketing’ module.

Let’s begin by creating the app/etc/modules file so the module will be enabled (note that we depend on the ‘Remarketing’ module as it’s integral to what we’re doing – we need the username and password.)



	
		
			true
			local
			
				
			
		
	

Secondly, the Listrak API method list can be found here. They use SOAP, so we can use the built in SOAP capabilities of PHP to communicate with their service. The first thing you will want to do is create a method which gets the SOAP client. In our helper file, we can put the following code:

public function getApiClient()
    {
        $username = Mage::getStoreConfig('remarketing/api/username');
        $password = Mage::getStoreConfig('remarketing/api/password');
        $WebServiceURL = 'http://webservices.listrak.com/v31/';
        $wsdl = "https://webservices.listrak.com/v31/IntegrationService.asmx?WSDL";

        if (file_get_contents($wsdl)) {
            $soapClient = new SoapClient($wsdl,
                array(
                    'soap_version' => SOAP_1_2,
                    'trace' => true
                )
            );
            $auth = array('UserName' => $username, 'Password' => $password);
            $header = new SoapHeader($WebServiceURL, "WSUser", $auth);
            $soapClient->__setSoapHeaders(array($header));

            Mage::register('listrakApiClient', $soapClient);

            return $soapClient;
        } else {
            return false;
        }
    }

We need the API credentials from the Remarketing module, so we get those first. We then check that the WSDL actually exists (in case Listrak happens to be down), and if so we create a new SOAP client and set its headers. We then use the built-in Magento register() function so we can register our new SOAP client and not have to reinitialize it every time we need it in the future.

It’s great that we have our client, but let’s make an actual API call. Perhaps one of the most basic API calls is to actually get a list within a Listrak account. If you refer to the API documentation, we’ll implement the function GetContactList. Here is the code to do so – note that I’ve called my module ‘Superlistrak’:

public function getList($listId)
    {
        $superListrakHelper = Mage::helper('superlistrak');
        $client = $superListrakHelper->getApiClient();
        if (!$client) {
            return false;
        }
        $params = array('ListID' => $listId);
        try {
            $response = $client->GetContactList($params);
            if (isset($response->WSException) && !is_null($response->WSException)) {
                if (isset($response->WSException->Description)) {
                    $superListrakHelper->log('GetContactList', $response->WSException->Description);
                }
                return false;
            }
        } catch (SoapFault $e) {
            $superListrakHelper->log('GetContactList', $e->;getMessage());
            return false;
        }

        return $response;
    }

You’ll notice that we get the client right away, and if it doesn’t exist, we exit right away thus preventing a future error. We then create our parameters to send to Listrak, and place a try catch to attempt to run the API method. If the response has a ‘WSException’, we know something went wrong with the call and therefore return false. If it also has a description, we can call our log function which can then give us a more detailed look at the error. If the SOAP call failed completely, we also log this. Finally, if everything worked correctly, we can return the response and use the list information however we want.

Our own log method which goes in helper is as follows:

public function log($action, $response, $email)
    {
        $msg = '';
        if ($email) {
            $msg .= 'Customer: ' . $email . ' - ';
        }
        $msg .= 'Action: ' . $action . ' - Received this response from the API: ' . $response;
        Mage::log($msg, null, 'Superlistrak.log');
    }

That’s it! You can create calls to other API method as you please now.

About Corey

Certified Magento Developer

Read more posts by Corey