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