Search This Blog

Mar 1, 2012

Listrak and PHP Integration

Listrak is a full service email firm founded in 1999 that provides solutions and services that increases the value of your customer’s email. They developed strategies and marketing solutions that help the company to drive their ROI (Return of Investment) to its full potential.
Like most email service company, Listrak build their own API in order to manipulate web services. Listrak used SOAP (Simple Object Access Protocol) and WSDL (Web Service Definition Language). They also recommend SoapUI, a tool that provides the ability to invoke, inspect and stimulate web services.

Requirements
In order to integrate Listrak API on PHP you need the following requirements:
a. Listrak Account (Username and Password).
b. PHP Soap library

PHP Integration Sample Code
<?php
/**
* LISTRAK INTEGRATION
* @Requirements 
* 1. Listrak Account, Username and Password
*/

 $sh_param = array(
    'UserName' => "GTan", 
    'Password' => "Admin12345"
   ); 
        
  $authvalues = new SoapVar($sh_param, SOAP_ENC_OBJECT);
        
  $headers[] = new SoapHeader("http://webservices.listrak.com/v31/", 'WSUser', $sh_param ); 
 
  $soapClient = new SoapClient("https://webservices.listrak.com/v31/IntegrationService.asmx?WSDL", array('trace'=> 1, 'exceptions' => true, 
        'cache_wsdl' => WSDL_CACHE_NONE, 'soap_version' => SOAP_1_2));
         
          
   $soapClient->__setSoapHeaders($headers); 
  
  
 $params = array(
             'WSContact' => array(
      'EmailAddress' => 'test@test.com', 
             'ListID' => 259048, 
      'ContactProfileAttribute' => array(
    array('AttributeID' => 1480218 , 'Value' => 'duanereade')
         
   )  
   ), 
      'ProfileUpdateType' => 'Overwrite',
      'ExternalEventIDs' => "",
      'OverrideUnsubscribe' => TRUE
      );
  
try {

     $rest = $soapClient->SetContact($params);
  //  echo $rest->SetContactResult;

} catch (SoapFault $e) {
 
    echo '<pre>';
     print($e->getMessage());
     echo '</pre>';

}

?>
Code Breakdown


Note: If you don't have any idea on how to use SOAP it is better to read PHP SOAP Manual first.
Here is the detailed explanation of the code:

First off is supply the correct listrak credential. 
$sh_param = array(
    'UserName' => "YourListrakUsername", 
    'Password' => "YourListrakPassword"
   ); 

The second thing is to prepare the SoapVar and SoapHeader. Listrak use "http://webservices.listrak.com/v31/" for the namespace of the header element and "WSUser" for the name of the SoapHeader Object.
$authvalues = new SoapVar($sh_param, SOAP_ENC_OBJECT);
$headers[] = new SoapHeader("http://webservices.listrak.com/v31/", 'WSUser', $sh_param ); 

Connect to Listrak SoapClient using the defined headers and Listrak credentials.
$soapClient = new SoapClient("https://webservices.listrak.com/v31/IntegrationService.asmx?WSDL", array('trace'=> 1, 'exceptions' => true, 
        'cache_wsdl' => WSDL_CACHE_NONE, 'soap_version' => SOAP_1_2));
There are two parameters that you need to pass when calling SoapClient. The first is the WSDL URL, Listrak WSDL is https://webservices.listrak.com/v31/IntegrationService.asmx?WSDL. The second parameter are array option of the SoapClient. In this parameter you can set up several options such as soap version, exceptions etc. Please refer PHP SoapClient for more info.

Next, you need to set the Soap Header using the following setSoapHeader Object.

$soapClient->__setSoapHeaders($headers); 

In order to subscribe or set contact info on Listrak, you need to used their defined web methods. View complete list.
In this particular code, I used setContact method. This method will attempts to subscribe or update a contact to a specified list of with profile data and external events.


     $params = array(
             'WSContact' => array(
      'EmailAddress' => 'test@test.com',  
             'ListID' => 259048, 
      'ContactProfileAttribute' => array(
    array('AttributeID' => 1480218 , 'Value' => 'duanereade')
         
   )  
   ), 
      'ProfileUpdateType' => 'Overwrite',
      'ExternalEventIDs' => "",
      'OverrideUnsubscribe' => TRUE
 );

You can view the complete information about setContact method here.

Now, the last part is to pass the parameter to setContact method.
  
try {

     $rest = $soapClient->SetContact($params);
   echo $rest->SetContactResult;

} catch (SoapFault $e) {
 
    echo '<pre>';
     print($e->getMessage());
     echo '</pre>';

}

4 comments:

Anonymous said...

Great tutorial! This is the first time working with PHP SOAP, and I'm trying to figure out how to use nusoap with Listrak (our current situation won't allow us to enable SOAP and recompile PHP). I'm having issues with overall merging nusoap tutorials with this one, but the problem I'm having now is setting the http encoding. I receive the error: Charset from HTTP Content-Type 'UTF-8' does not match encoding from XML declaration 'iso-8859-1'. Could you possibly point me in the right direction?Thanks!

Janz said...

From what I understand you're having a problem with the content type. I can't give a clear advice here though but please check you content type charset declaration on WSDL or in PHP. Are you using the WSDL of listrak?

If you want, you can send me your code through my contact page.

Thanks,

Janzell

Unknown said...

Hey Hi , This is great tutorial and works fine for me, Thanks.

I have query that can i store multiple emails with attribute in SetContact() method.

Please reply.

Thanks again.

Unknown said...

Hey Hi , This is great tutorial and works fine for me, Thanks.

I have query that can i store multiple emails with attribute in SetContact() method.

Please reply.

Thanks again.