PHP

Vtrenz (EngageB2B) SOAP API PHP Library

Added by on August 31st, 2010, filed under PHP, Portfolio, Web Development, XML

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

1
2
3
4
5
6
7
8
9
10
11
$options = array('debug' => true);
$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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class Vtrenz_Api {

    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('<incoming-requests></incoming-requests>');
        $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__);
    }
}

Enjoy this Post?

Spread the word by promoting this post on FaceBook and Twitter.

What do you think?