-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinterlink.php
330 lines (311 loc) · 9.23 KB
/
interlink.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* Interlink express API class
* by drgr33n <zarren@norfolklights.com>
* Adapted to PSR-2 by AbanteCart Team dsuprunenko@abantecart.com
*
*/
class interlink
{
// Initialize variables
protected $version = "Interlink express API class v1.1a";
protected $url;
protected $timeout = 5;
protected $ch;
protected $headers;
protected $username;
protected $password;
protected $accountNo;
protected $jsonSize = 0;
protected $returnFormat = 'application/json';
protected $contentType = 'none';
protected $isPrintJob = 0;
protected $payload = '';
/**
* interlink constructor.
*
* @param string $url
* @param string $username
* @param string $password
* @param string|int $accountNo
*/
public function __construct($url, $username, $password, $accountNo)
{
$this->url = $url;
$this->username = $username;
$this->password = $password;
$this->accountNo = $accountNo;
$this->ch = curl_init();
}
/**
* @param string $timeout
* @param array $headers
*
* @return array
* @throws Exception
*/
protected function authenticate($timeout = '5', $headers = array())
{
$headers = (array)$headers;
$headers['Content-Type'] = 'Content-Type: None';
$headers['Accept'] = 'Accept: application/json';
$headers['Authorization'] = 'Authorization: Basic '.base64_encode($this->username.':'.$this->password);
$headers['GEOClient'] = 'GEOClient: '.$this->username.'/'.$this->accountNo;
$headers['Content-Length'] = 'Content-Length: 0';
curl_setopt_array(
$this->ch,
array(
CURLOPT_URL => $this->url.'/user/?action=login',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_USERAGENT => $this->version,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => 'POST',
)
);
$authPost = curl_exec($this->ch);
$httpCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
$data = json_decode($authPost, true);
if (curl_errno($this->ch)) {
throw new Exception('Error connecting to API: '.curl_error($this->ch));
} elseif ($httpCode === 401 || $httpCode === 403 || $httpCode === 404 || $httpCode === 500
|| $httpCode === 503) {
$this->httpError($httpCode);
} else {
return $data['data']['geoSession'];
}
return array();
}
/**
* @param array $headers
*
* @throws Exception
*/
protected function constructHeaders($headers = array())
{
$headers = (array)$headers;
$authToken = $this->authenticate();
$this->headers = array_merge(array(
'Content-Type: '.$this->contentType,
'Accept: '.$this->returnFormat,
'GEOClient: '.$this->username.'/'.$this->accountNo,
'GEOSession: '.$authToken,
'Content-Length: '.$this->jsonSize,
), $headers);
}
/**
* @return array
* @throws Exception
*/
public function listCountry()
{
$method = "GET";
$route = "/shipping/country";
$query = $this->doQuery($method, $route);
if (isset($query['error'])) {
$this->apiError($query['error']);
}
return $query;
}
/**
* @param $route
*
* @return array
* @throws Exception
*/
public function customGet($route)
{
$method = "GET";
$query = $this->doQuery($method, $route);
if (isset($query['error'])) {
$this->apiError($query['error']);
}
return $query;
}
/**
* @param array $data
*
* @return array
* @throws Exception
*/
public function getShipping($data)
{
$method = "GET";
// Needs cleaning up but regex is like Chinese to me !
$data = str_replace('%5D', '', str_replace('%5B', '.', http_build_query($data)));
$route = "/shipping/network/?".$data;
$query = $this->doQuery($method, $route);
if (isset($query['error'])) {
$this->apiError($query['error']);
}
return $query;
}
/**
* @param string $country - two letters country code
*
* @return array
* @throws Exception
*/
public function getCountry($country)
{
$method = "GET";
$route = "/shipping/country/";
$query = $this->doQuery($method, $route.$country);
if (isset($query['error'])) {
$this->apiError($query['error']);
}
return $query;
}
/**
* @param string $geoCode
*
* @return array
* @throws Exception
*/
public function getNetCode($geoCode)
{
$method = "GET";
$route = "/shipping/network/".$geoCode;
$query = $this->doQuery($method, $route);
if (isset($query['error'])) {
$this->apiError($query['error']);
}
return $query;
}
/**
* @param mixed $payload
*
* @return array
* @throws Exception
*/
public function insertShipping($payload)
{
$method = "POST";
$route = "/shipping/shipment";
$this->encodePayload($payload);
$this->contentType = 'application/json';
$query = $this->doQuery($method, $route);
if (isset($query['error'])) {
$this->apiError($query['error']);
}
return $query;
}
/**
* @param string $shipmentId
* @param string $dataType
*
* @return array
* @throws Exception
*/
public function getLabel($shipmentId, $dataType)
{
$method = "GET";
$this->returnFormat = $dataType;
$route = "/shipping/shipment/".$shipmentId."/label/";
$this->isPrintJob = 1;
$this->contentType = 'Accept';
$query = $this->doQuery($method, $route);
if (isset($query['error'])) {
$this->apiError($query['error']);
}
return $query;
}
/**
* @param string $method
* @param string $route
*
* @return array
* @throws Exception
*/
protected function doQuery($method, $route)
{
$this->constructHeaders();
curl_setopt_array($this->ch, array(
CURLOPT_URL => $this->url.$route,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => $this->timeout,
CURLOPT_USERAGENT => $this->version,
CURLOPT_HTTPHEADER => $this->headers,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => (isset($this->payload)) ? $this->payload : null,
));
$data = curl_exec($this->ch);
$httpCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
//If print job don't decode.
if ($this->isPrintJob == 0) {
$response = json_decode($data, true);
} else {
$response = $data;
}
if (curl_errno($this->ch)) {
throw new Exception('Curl Error connecting to API: '.curl_error($this->ch));
} elseif ($httpCode === 401 || $httpCode === 403 || $httpCode === 404 || $httpCode === 500
|| $httpCode === 503) {
$this->httpError($httpCode);
} else {
return $response;
}
return array();
}
// Encode payload
/**
* @param mixed $payload
*/
protected function encodePayload($payload)
{
$this->payload = json_encode($payload);
$this->jsonSize = strlen($this->payload);
}
/**
* @param $httpCode
*
* @throws Exception
*/
public function httpError($httpCode)
{
switch ($httpCode) {
case '401':
throw new Exception('Username / Password incorrect');
break;
case '403':
throw new Exception('Geo-session header not found or invalid');
break;
case '404':
throw new Exception('An attempt was made to call an API in which the URL cannot be found');
break;
case '500':
throw new Exception('The ESG server had an internal error');
break;
case '503':
throw new Exception('The API being called is temporary out of service');
break;
}
}
/**
* @param array $err
*
* @throws Exception
*/
public function apiError($err)
{
// Probably not the ideal solution but works and I'm not really a PHP dev. Please clean me !! :D
if (isset($err[0])) {
throw new Exception(
'API Error! Code: '.$err[0]['errorCode']
.' Type: '.$err[0]['errorType']
.' Message: '.$err[0]['obj'].' / '.$err[0]['errorMessage']);
} else {
throw new Exception(
'API Error! Code: '.$err['errorCode']
.' Type: '.$err['errorType']
.' Message: '.$err['obj'].' / '.$err['errorMessage']);
}
}
/**
* Destruct object
*/
public function __destruct()
{
curl_close($this->ch);
}
}