|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pakkelabels |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Pakkelabels |
|
8
|
|
|
* @package Pakkelabels |
|
9
|
|
|
* @author Lars Olesen <[email protected]> |
|
10
|
|
|
* @author Hasse Ramlev Hansen <[email protected]> |
|
11
|
|
|
* @copyright 2015 Lars Olesen |
|
12
|
|
|
* @license MIT Open Source License https://opensource.org/licenses/MIT |
|
13
|
|
|
* @version GIT: <git_id> |
|
14
|
|
|
* @link http://github.com/discimport/pakkelabels-dk |
|
15
|
|
|
*/ |
|
16
|
|
|
namespace Pakkelabels; |
|
17
|
|
|
|
|
18
|
|
|
use Pakkelabels\Exception\PakkelabelsException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Pakkelabels |
|
22
|
|
|
* |
|
23
|
|
|
* Usage: |
|
24
|
|
|
* ---------------- |
|
25
|
|
|
* The first thing required is to login |
|
26
|
|
|
* $label = new Pakkelabels('api_user', 'api_key'); |
|
27
|
|
|
* |
|
28
|
|
|
* This will login and fetch the required token. |
|
29
|
|
|
* The token is then automatically added to any subsequent calls. |
|
30
|
|
|
* |
|
31
|
|
|
* To see the generated token you can use: |
|
32
|
|
|
* echo $label->getToken(); |
|
33
|
|
|
* |
|
34
|
|
|
* Examples: |
|
35
|
|
|
* ---------------- |
|
36
|
|
|
* // Get all Post Danmark labels shipped to Denmark |
|
37
|
|
|
* $labels = $label->shipments(array('shipping_agent' => 'pdk', 'receiver_country' => 'DK')); |
|
38
|
|
|
* |
|
39
|
|
|
* // Display the PDF for a specific label |
|
40
|
|
|
* $base64 = $label->pdf(31629); |
|
41
|
|
|
* $pdf = base64_decode($base64); |
|
42
|
|
|
* header('Content-type: application/pdf'); |
|
43
|
|
|
* header('Content-Disposition: inline; filename="label.pdf"'); |
|
44
|
|
|
* echo $pdf; |
|
45
|
|
|
* |
|
46
|
|
|
* @category Pakkelabels |
|
47
|
|
|
* @package Pakkelabels |
|
48
|
|
|
* @author Lars Olesen <[email protected]> |
|
49
|
|
|
* @copyright 2015 Lars Olesen |
|
50
|
|
|
* @license http://opensource.org/licenses/bsd-license.php New BSD License |
|
51
|
|
|
* @link http://github.com/discimport/pakkelabels-dk |
|
52
|
|
|
*/ |
|
53
|
|
|
|
|
54
|
|
|
class Pakkelabels |
|
55
|
|
|
{ |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* API Endpoint URL |
|
59
|
|
|
* |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
const API_ENDPOINT = 'https://app.pakkelabels.dk/api/public/v2'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* API Endpoint URL |
|
66
|
|
|
* |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
const VERSION = '1.1'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* API user |
|
73
|
|
|
* |
|
74
|
|
|
* @var string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $api_user; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* API key |
|
80
|
|
|
* |
|
81
|
|
|
* @var string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $api_key; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Token |
|
87
|
|
|
* |
|
88
|
|
|
* @var string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $token; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Constructor |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $api_user |
|
96
|
|
|
* @param string $api_key |
|
97
|
|
|
* |
|
98
|
|
|
* @throws \PakkelabelsException |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function __construct($api_user, $api_key) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$this->api_user = $api_user; |
|
103
|
1 |
|
$this->api_key = $api_key; |
|
104
|
1 |
|
$this->login(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Login |
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
* @throws \PakkelabelsException |
|
112
|
|
|
*/ |
|
113
|
1 |
|
protected function login() |
|
114
|
|
|
{ |
|
115
|
1 |
|
$result = $this->makeApiCall( |
|
116
|
1 |
|
'users/login', |
|
117
|
1 |
|
true, |
|
118
|
1 |
|
array('api_user' => $this->api_user, 'api_key' => $this->api_key) |
|
119
|
1 |
|
); |
|
120
|
|
|
$this->token = $result['token']; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get balance |
|
125
|
|
|
* |
|
126
|
|
|
* @return void |
|
127
|
|
|
* @throws \PakkelabelsException |
|
128
|
|
|
*/ |
|
129
|
|
|
public function balance() |
|
130
|
|
|
{ |
|
131
|
|
|
$result = $this->makeApiCall('users/balance'); |
|
132
|
|
|
return $result['balance']; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get PDF |
|
137
|
|
|
* |
|
138
|
|
|
* @return base64 encoded string |
|
139
|
|
|
* @throws \PakkelabelsException |
|
140
|
|
|
*/ |
|
141
|
|
|
public function pdf($id) |
|
142
|
|
|
{ |
|
143
|
|
|
$result = $this->makeApiCall('shipments/pdf', false, array('id' => $id)); |
|
144
|
|
|
return $result['base64']; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get pickup points. |
|
149
|
|
|
* |
|
150
|
|
|
* @param array $params |
|
151
|
|
|
* |
|
152
|
|
|
* @return mixed |
|
153
|
|
|
* @throws \PakkelabelsException |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getPickupPoints($params = array()) |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->makeApiCall('pickup_points', false, $params); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Search shipments |
|
162
|
|
|
* |
|
163
|
|
|
* @param array $params |
|
164
|
|
|
* |
|
165
|
|
|
* @return mixed |
|
166
|
|
|
* @throws \PakkelabelsException |
|
167
|
|
|
*/ |
|
168
|
|
|
public function shipments($params = array()) |
|
169
|
|
|
{ |
|
170
|
|
|
$result = $this->makeApiCall('shipments/shipments', false, $params); |
|
171
|
|
|
return $result; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Get imported shipments |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $params |
|
178
|
|
|
* |
|
179
|
|
|
* @return mixed |
|
180
|
|
|
* @throws \PakkelabelsException |
|
181
|
|
|
*/ |
|
182
|
|
|
public function importedShipments($params = array()) |
|
183
|
|
|
{ |
|
184
|
|
|
$result = $this->makeApiCall('shipments/imported_shipments', false, $params); |
|
185
|
|
|
return $result; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Create imported shipment |
|
190
|
|
|
* |
|
191
|
|
|
* @param array $params |
|
192
|
|
|
* |
|
193
|
|
|
* @return mixed |
|
194
|
|
|
* @throws \PakkelabelsException |
|
195
|
|
|
*/ |
|
196
|
|
|
public function createImportedShipment($params) |
|
197
|
|
|
{ |
|
198
|
|
|
$result = $this->makeApiCall('shipments/imported_shipment', true, $params); |
|
199
|
|
|
return $result; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Create shipment |
|
204
|
|
|
* |
|
205
|
|
|
* @param array $params |
|
206
|
|
|
* |
|
207
|
|
|
* @return mixed |
|
208
|
|
|
* @throws \PakkelabelsException |
|
209
|
|
|
*/ |
|
210
|
|
|
public function createShipment($params) |
|
211
|
|
|
{ |
|
212
|
|
|
$result = $this->makeApiCall('shipments/shipment', true, $params); |
|
213
|
|
|
return $result; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get freight rates |
|
218
|
|
|
* |
|
219
|
|
|
* @return mixed |
|
220
|
|
|
* @throws \PakkelabelsException |
|
221
|
|
|
*/ |
|
222
|
|
|
public function freightRates() |
|
223
|
|
|
{ |
|
224
|
|
|
$result = $this->makeApiCall('shipments/freight_rates'); |
|
225
|
|
|
return $result; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get payment requests |
|
230
|
|
|
* |
|
231
|
|
|
* @return mixed |
|
232
|
|
|
* @throws \PakkelabelsException |
|
233
|
|
|
*/ |
|
234
|
|
|
public function paymentRequests() |
|
235
|
|
|
{ |
|
236
|
|
|
$result = $this->makeApiCall('users/payment_requests'); |
|
237
|
|
|
return $result; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Get GLS Droppoints |
|
242
|
|
|
* |
|
243
|
|
|
* @param array $params |
|
244
|
|
|
* |
|
245
|
|
|
* @return mixed |
|
246
|
|
|
* @throws \PakkelabelsException |
|
247
|
|
|
*/ |
|
248
|
|
|
public function glsDroppoints($params) |
|
249
|
|
|
{ |
|
250
|
|
|
$result = $this->makeApiCall('shipments/gls_droppoints', false, $params); |
|
251
|
|
|
return $result; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Get PostDK Droppoints |
|
256
|
|
|
* |
|
257
|
|
|
* @param array $params |
|
258
|
|
|
* |
|
259
|
|
|
* @return mixed |
|
260
|
|
|
* @throws \PakkelabelsException |
|
261
|
|
|
*/ |
|
262
|
|
|
public function pdkDroppoints($params) |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->makeApiCall('shipments/pdk_droppoints', false, $params); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Get token |
|
269
|
|
|
* |
|
270
|
|
|
* @return string |
|
271
|
|
|
*/ |
|
272
|
|
|
public function getToken() |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->token; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Make API Call |
|
279
|
|
|
* |
|
280
|
|
|
* @param string $method |
|
281
|
|
|
* @param boolean $doPost |
|
282
|
|
|
* @param array $params |
|
283
|
|
|
* |
|
284
|
|
|
* @return mixed |
|
285
|
|
|
* @throws \PakkelabelsException |
|
286
|
|
|
*/ |
|
287
|
1 |
|
protected function makeApiCall($method, $doPost = false, $params = array()) |
|
288
|
|
|
{ |
|
289
|
1 |
|
$ch = curl_init(); |
|
290
|
1 |
|
$params['token'] = $this->token; |
|
291
|
1 |
|
$params['user_agent'] = 'pdk_php_library v' . self::VERSION; |
|
292
|
|
|
|
|
293
|
1 |
|
$query = http_build_query($params); |
|
294
|
1 |
|
if ($doPost === true) { |
|
295
|
1 |
|
curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT . '/' . $method); |
|
296
|
1 |
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
297
|
1 |
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); |
|
298
|
1 |
|
} else { |
|
299
|
|
|
curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT . '/' . $method . '?' . $query); |
|
300
|
|
|
} |
|
301
|
1 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
302
|
|
|
|
|
303
|
1 |
|
$output = curl_exec($ch); |
|
304
|
1 |
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
305
|
1 |
|
curl_close($ch); |
|
306
|
|
|
|
|
307
|
1 |
|
$output = json_decode($output, true); |
|
308
|
1 |
|
if ($http_code != 200) { |
|
309
|
1 |
|
throw new PakkelabelsException($output['message']); |
|
310
|
|
|
} |
|
311
|
|
|
return $output; |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|