-
Notifications
You must be signed in to change notification settings - Fork 0
/
SilverpopConnection.php
176 lines (149 loc) · 4.89 KB
/
SilverpopConnection.php
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
<?php
/**
* @file
* Contains definition of the SilverpopConnection class.
*/
/**
* Class definition for Silverpop API connection.
*/
class SilverpopConnection {
// Silverpop API endpoint URL.
protected $endpoint;
// Silverpop API username.
protected $username;
// Silverpop API password.
protected $password;
// Silverpop API session id.
protected $sessionID;
// A log of all calls made with this instance.
public $sessionLog = array();
// A log of all <Fault> messages returned through this instance.
public $faultLog = array();
// A boolean flag for whether or not to perform transaction logging.
public $logTransactions = FALSE;
// A boolean flag for whether or not to perform Silverpop <Fault> logging.
public $logFaults = FALSE;
/**
* Constructor for SilverpopConnection class.
*
* @param string $endpoint
* the URL to connect to when calling API
* @param string $username
* the username to authenticate with when logging in
* @param string $password
* the password to authenticate with when logging in
*
* @return object
* a new SilverpopConnection object instance
*/
public function __construct($endpoint, $username, $password) {
$this->endpoint = $endpoint;
$this->username = $username;
$this->password = $password;
$this->login();
return $this;
}
/**
* Public wrapper for making an API call to the Silverpop XMLAPI endpoint.
*
* @param string $xml
* the name of the API function to execute
*
* @return mixed
* string: XML response from the Silverpop endpoint
* FALSE: failure to connect
*/
public function call($xml) {
// Add session id to API calls that require it.
$url = ($this->sessionID) ? $this->endpoint . ';jsessionid=' . $this->sessionID : $this->endpoint;
return $this->getXMLResponse($url, $xml);
}
/**
* Send processed XML via curl to Silverpop XMLAPI.
*/
protected function getXMLResponse($url, $xml) {
// Initialize curl call.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml;charset=UTF-8',
'Content-Length: ' . strlen($xml),
));
// Perform curl call.
$call_time = microtime(TRUE);
$response = curl_exec($ch);
$response_time = microtime(TRUE);
// Log all API calls.
if ($this->logTransactions) {
$this->sessionLog[] = array(
'call_time' => date('Y/m/d [g:i:sa]', $call_time),
'executed_in' => ($response_time - $call_time) . ' seconds',
'call_xml' => $xml,
'response_xml' => $response,
);
}
// Log all <Fault> tags.
if ($this->logFaults) {
$xml = new DOMDocument();
$xml->loadXML($response);
$fault_tags = $xml->getElementsByTagName('FaultString');
for ($i = 0; $i < $fault_tags->length; $i++) {
$this->faultLog[] = $fault_tags->item($i)->nodeValue;
}
}
if ($response) {
curl_close($ch);
}
return $response;
}
/**
* Create a login request and retrieve a session id.
*
* ID is stored in $this->sessionID for use by all subsequent requests.
*/
protected function login() {
$login_response = $this->call('
<Envelope>
<Body>
<Login>
<USERNAME>' . $this->username . '</USERNAME>
<PASSWORD>' . $this->password . '</PASSWORD>
</Login>
</Body>
</Envelope>
');
if (!$login_response) {
// Connection failure (curl returned FALSE).
throw new SilverpopConnectionException('Could not find the Silverpop XMLAPI at the address "' . $this->endpoint . '".');
}
$xml = new DOMDocument();
$xml->loadXML($login_response);
$fault_tags = $xml->getElementsByTagName('FaultString');
$fault_codes = $xml->getElementsByTagName('errorid');
$id_tag = $xml->getElementsByTagName('SESSIONID');
// Received a fault response from Silverpop.
if ($fault_tags->length > 0) {
$message = $fault_tags->item(0)->nodeValue;
$code = $fault_codes->item(0)->nodeValue;
throw new SilverpopConnectionException('Could not connect to the Silverpop XMLAPI. Silverpop says: "' . $message . '".', $code);
}
// No fault response and no session ID -- bad endpoint.
elseif ($id_tag->length == 0) {
throw new SilverpopConnectionException('Could not find the Silverpop XMLAPI at the address "' . $this->endpoint . '".');
}
// Successfully retrieved session ID.
else {
$this->sessionID = $id_tag->item(0)->nodeValue;
}
}
/**
* Send a logout request to Silverpop API.
*/
protected function logout() {
$logout_response = $this->call('<Envelope><Body></Logout></Body></Envelope>');
}
}