Below you will find a class for using the Vtrenz Web Services API with PHP. This class abstracts a few of the individual methods available via the soap-based api and takes care of writing all of the XML in your request for you (ala SimpleXML).
Here are the supported methods:
- insertCampaignParticipant
- sendOneOffEmail
- insertContact
- getEmailMessageStatus
- getContactData
I’ve even written a debug method that when turned on, while display the request, the response and both the response and request times (for proving how incredibly slow Vtrenz is).
Here’s a basic example:
Get a users iMarketingSyncID using their email address
$databaseID = '';
$vtrenz = new Vtrenz_Api($options);
$contact = $vtrenz->getContactData($databaseID,'','', $usersEmailAddress);
return $contact->{'single-response'}
->{'outgoing-responses'}
->response
->{'outgoing-data'}
->{'contact'}
->field[1]; //returns the iMarketingSyncID field
Below is the actual source for the library. Be sure to fill in your own credentials.
The Vtrenz PHP Class
private $username = '';
private $password = '';
private $wsdl= 'http://services.vtrenz.net/receiver.cfc?wsdl';
private $xml = NULL;
private $incomingData = NULL;
private $request = NULL;
private $_debugMode = false;
public function __construct($options = null) {
if ($options !== null) {
$this->setOptions($options);
}
$this->xml = new SimpleXMLElement('');
$this->request = $this->xml->addChild('request');
$this->incomingData = $this->request->addChild('incoming-data');
}
/**
* Set Options
*
* Allows setting options as an associative array of option => value pairs.
*
* @param array $options
*/
private function setOptions($options) {
foreach ($options as $key => $value) {
switch ($key) {
case 'debug':
$this->setDebugMode($value);
break;
}
}
}
/**
* Set debug mode
*
* @param bool $value
*/
private function setDebugMode($value) {
$this->_debugMode = $value;
}
/**
* Self destruct then re-instantiate
*/
private function destruct() {
unset($this->xml);
$this->__construct();
}
/**
* Transmit SOAP Call
*
* @param string $method
* @return SimpleXMLElement
*/
private function transmit($method) {
$this->request->addAttribute('description', $method);
$startTime = date('r');
$this->client = new Zend_Soap_Client($this->wsdl, array('soap_version' => SOAP_1_1));
$result = $this->client->incomingRequest($this->username, $this->password, $this->xml->asXML());
if($this->_debugMode)
$this->printDebug($method, $this->client->getLastRequest(), $result, $startTime);
$this->destruct();
return new SimpleXMLElement($result,LIBXML_NOCDATA);
}
/**
* Print debug info
*
* @param string $method
* @param string $lastRequest
* @param string $result
* @param string $startTime
*/
private function printDebug($method, $lastRequest, $result, $startTime) {
echo "<h1>Vtrend Debug Info</h1>\n\r";
echo "<h2>". $method ."</h2>\n\r";
echo "<h4>Request</h4>\n\r";
echo $lastRequest;
echo "<h4>Response</h4>\n\r";
echo $result;
echo "<h4>Timestamp</h4>\n\r";
echo "<h5>Start Time</h5>\n\r";
echo $startTime;
echo "<h5>Stop Time</h5>\n\r";
echo date('r');
}
/**
* Inserts a contact to an active campaign in the Vtrenz marketing database
*
* @param string $campaignID
* @param string $contacts
* @return SimpleXMLElement
*/
public function insertCampaignParticipant($campaignID, $contacts = array()) {
$campaign = $this->incomingData->addChild('campaign');
$campaign->addAttribute('campaignID', $campaignID);
$i = 0;
foreach($contacts as $contact) {
$contactNode[$i] = $campaign->addChild('contact');
foreach($contact as $key => $val) {
$contactNode[$i]->addAttribute($key, $val);
}
$i++;
}
return $this->transmit(__FUNCTION__);
}
/**
* Sends a one time email from from Vtrenz
*
* @param string $messageID
* @param array $contacts
* @param array $settings
* @param array $linkOptions
* @return SimpleXMLElement
*/
public function sendOneOffEmail($messageID, $contacts = array(), $settings = NULL, $linkOptions = NULL) {
$email = $this->incomingData->addChild('email-message');
$email->addAttribute('messageID', $messageID);
if(isset($settings)) {
$settingsNode = $email->addChild('settings');
foreach($settings as $key => $val) {
$settingsNode->addAttribute($key, $val);
}
}
if(isset($linkOptions)) {
$options = $email->addChild('link-options');
foreach($linkOptions as $key => $val) {
$options->addAttribute($key, $val);
}
}
$i = 0;
foreach($contacts as $contact) {
$contactNode[$i] = $email->addChild('contact');
foreach($contact as $key => $val) {
$contactNode[$i]->addAttribute($key, $val);
}
$i++;
}
return $this->transmit(__FUNCTION__);
}
public function insertContact($dbID, $fields = array()) {
$contact = $this->incomingData->addChild('contact');
$contact->addAttribute('databaseID', $dbID);
$i = 0;
foreach($fields as $key => $val) {
$field[$i] = $contact->addChild('field', $val);
$field[$i]->addAttribute('id', $key);
$i++;
}
return $this->transmit(__FUNCTION__);
}
public function getEmailMessageStatus($messageID) {
$emailMessage = $this->incomingData->addChild('email-message');
$emailMessage->addAttribute('messageID', $messageID);
return $this->transmit(__FUNCTION__);
}
public function getContactData($dbID, $iMarketingSyncID = NULL, $syncID = NULL, $email = NULL, $webSyncID = NULL ) {
$contact = $this->incomingData->addChild('contact');
$contact->addAttribute('databaseID', $dbID);
if(isset($iMarketingSyncID))
$contact->addAttribute('iMarketingSyncID', $iMarketingSyncID);
if(isset($syncID))
$contact->addAttribute('syncID', $syncID);
if(isset($email))
$contact->addAttribute('email', $email);
if(isset($webSyncID))
$contact->addAttribute('webSyncID', $webSyncID);
return $this->transmit(__FUNCTION__);
}
}
No Comments