This repository was archived by the owner on Dec 4, 2025. It is now read-only.
forked from kornrunner/returnpath-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.returnpath.php
More file actions
293 lines (267 loc) · 9.21 KB
/
class.returnpath.php
File metadata and controls
293 lines (267 loc) · 9.21 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* Return Path API client library
*
* Please consult https://api.returnpath.com/docs/index.html
*/
require_once dirname(__FILE__) . '/class.returnpathresponse.php';
/**
* Class to manage Return Path API access
*
*/
class ReturnPath
{
protected $responseHeaders;
protected $requestHeaders;
protected $username;
protected $password;
protected $saveHeaders;
protected $ssl;
protected $endPoint;
protected $apiVersion;
protected $lastResponse;
protected $product;
protected $authenticationMethod;
protected $authenticationMethods = array('private', 'http_basic');
/**
* Instantiate a new object.
*/
function __construct($username, $password, $authenticationMethod='http_basic')
{
$this->username = $username;
$this->password = $password;
$this->saveHeaders = false;
$this->ssl = true;
$this->endPoint = 'api.returnpath.com';
$this->apiVersion = 'v1';
$this->lastResponse = null;
$this->product = null;
if (! in_array($authenticationMethod, $this->authenticationMethods)) {
throw new InvalidArgumentException("Invalid authentication method '$authenticationMethod''");
}
$this->authenticationMethod = $authenticationMethod;
}
/**
* Specify the product.
* @param string $product
* @return boolean success
*/
public function setProduct($product)
{
if (! in_array($product, array('cert', 'ecm', 'im', 'preview', 'repmon'))) {
throw new InvalidArgumentException("Invalid product '$product'");
}
$this->product = $product;
return true;
}
/**
* Executes HTTP GET request
* @param string $action
* @param null $parameters - specify as associative array
* @param null $product
* @return bool|ReturnPathResponse
* @throws InvalidArgumentException
*/
public function get($action = '', $parameters = null, $product = null)
{
if (is_null($this->product) && is_null($product)) {
throw new InvalidArgumentException("you must specify a product");
}
return $this->_doCall('GET', $action, $parameters);
}
/**
* Executes HTTP PUT request
* @param string $action
* @param null $parameters - specify as associative array
* @param null $product
* @return bool|ReturnPathResponse
* @throws InvalidArgumentException
*/
public function put($action, $parameters = null, $product = null)
{
if (is_null($this->product) && is_null($product)) {
throw new InvalidArgumentException("you must specify a product");
}
return $this->_doCall('PUT', $action, $parameters);
}
/**
* Executes HTTP POST request
* @param string $action
* @param null $parameters - specify as associative array
* @param null $product
* @return bool|ReturnPathResponse
* @throws InvalidArgumentException
*/
public function post($action = '', $parameters = null, $product = null)
{
if (is_null($this->product) && is_null($product)) {
throw new InvalidArgumentException("you must specify a product");
}
return $this->_doCall('POST', $action, $parameters);
}
/**
* Executes HTTP DELETE request
* @param string $action
* @param null $parameters - specify as associative array
* @param null $product
* @return bool|ReturnPathResponse
* @throws InvalidArgumentException
*/
public function delete($action = '', $parameters = null, $product = null)
{
if (is_null($this->product) && is_null($product)) {
throw new InvalidArgumentException("you must specify a product");
}
return $this->_doCall('DELETE', $action, $parameters);
}
/**
* Specify the API endpoint.
* If setEndPoint isn't used, then api.returnpath.com is used by default
* @param string $endPoint
* @return boolean success
*/
public function setEndPoint($endPoint)
{
$this->endPoint = $endPoint;
return true;
}
/**
* Specify whether or not you want to keep a trace of HTTP headers
* default is to not keep a trace of HTTP headers
* @param bool $yes Set to true to keep a trace of HTTP headers
*/
public function saveHeaders($yes = true)
{
$this->saveHeaders = $yes;
}
/**
* Returns the ReturnPathResponse object for the last API call.
* @return ReturnPathResponse
*/
public function getLastResponse()
{
return $this->lastResponse;
}
/**
* Internal method returning the call signature
* @param $url
* @return string
*/
protected function getPublicPrivateSignature($url)
{
$timestamp = time();
$path = parse_url($url, PHP_URL_PATH);
$hash = hash_hmac('sha256', "$path:$timestamp", $this->password, false);
return base64_encode($this->username . ":$hash:$timestamp");
}
/**
* Internal method handling HTTP request
* @param $httpMethod
* @param $action
* @param null $parameters
* @param null $product
* @return bool|ReturnPathResponse
*/
protected function _doCall($httpMethod, $action, $parameters = null, $product = null)
{
$url = 'http';
if ($this->ssl) {
$url = 'https';
}
$url .= '://' . $this->endPoint . '/';
if ($this->apiVersion != '') {
$url .= $this->apiVersion . '/';
}
$url .= (is_null($product) ? $this->product : $product) . "/$action";
if ($this->authenticationMethod == "private") {
$url .= '?' . "api_digest=" . $this->getPublicPrivateSignature($url);
}
$httpHeadersToSet = array();
if (is_array($parameters)) {
$newParams = '';
foreach ($parameters as $key => $value) {
if ($newParams != '') {
$newParams .= '&';
}
if (!is_array($value)) {
$newParams .= "$key=" . urlencode($value);
} else {
foreach ($value as $currentValue) {
$newParams .= $key . '[]=' . urlencode($currentValue);
}
}
}
$parameters = $newParams;
}
if (($httpMethod == 'GET') && ! is_null($parameters) && !empty($parameters)) {
$url .= (($this->authenticationMethod == "private") ? '&' : '?') . $parameters;
}
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Return Path API PHP');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
if ($this->authenticationMethod == "http_basic") {
curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':');
}
if ($this->ssl) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
}
if ($httpMethod != 'GET') {
if ($httpMethod == 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
if (!is_null($parameters)) {
$httpHeadersToSet[] = 'Content-Length: ' . strlen($parameters);
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
} else {
$httpHeadersToSet[] = 'Content-Length: 0';
}
} else {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpMethod);
if ($httpMethod == 'PUT') {
if (is_string($parameters)) {
$httpHeadersToSet[] = 'Content-Length: ' . strlen($parameters);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
}
}
}
if (count($httpHeadersToSet) > 0) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeadersToSet);
}
if ($this->saveHeaders) {
$this->responseHeaders = array();
$this->requestHeaders = array();
curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($this, '_setHeader'));
curl_setopt($curl, CURLINFO_HEADER_OUT, 1);
}
$result = curl_exec($curl);
$httpHeadersIn = ($this->saveHeaders) ? $this->responseHeaders : null;
$httpHeadersOut = ($this->saveHeaders) ? preg_split('/(\\n|\\r){1,2}/', curl_getinfo($curl, CURLINFO_HEADER_OUT)) : null;
$acceptableContentTypes = array('application/json');
$response = new ReturnPathResponse(
curl_getinfo($curl, CURLINFO_HTTP_CODE),
$httpHeadersOut,
$httpHeadersIn,
curl_getinfo($curl, CURLINFO_CONTENT_TYPE),
$result,
$acceptableContentTypes);
curl_close($curl);
if ($response->hasError()) {
$this->lastResponse = $response;
return false;
}
return $response;
}
/**
* Internal method used to build HTTP header trace
* @param $curl
* @param $headers
* @return int
*/
public function _setHeader($curl, $headers)
{
$this->responseHeaders[] = trim($headers, "\n\r");
return strlen($headers);
}
}
?>