1 | <?php |
||
2 | |||
3 | namespace Bpost\BpostApiClient; |
||
4 | |||
5 | use Bpost\BpostApiClient\ApiCaller\ApiCaller; |
||
6 | use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostCurlException; |
||
7 | use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostInvalidXmlResponseException; |
||
8 | use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostTaxipostLocatorException; |
||
9 | use Bpost\BpostApiClient\Geo6\Poi; |
||
10 | use SimpleXMLElement; |
||
11 | |||
12 | /** |
||
13 | * Geo6 class |
||
14 | * |
||
15 | * @author Tijs Verkoyen <[email protected]> |
||
16 | * |
||
17 | * @version 3.0.0 |
||
18 | * |
||
19 | * @copyright Copyright (c), Tijs Verkoyen. All rights reserved. |
||
20 | * @license BSD License |
||
21 | */ |
||
22 | class Geo6 |
||
23 | { |
||
24 | // URL for the api |
||
25 | const API_URL = 'https://pudo.bpost.be/Locator'; |
||
26 | |||
27 | // current version |
||
28 | const VERSION = '3'; |
||
29 | |||
30 | /** |
||
31 | * @see getPointType |
||
32 | * @see getServicePointPageUrl |
||
33 | */ |
||
34 | const POINT_TYPE_POST_OFFICE = 1; |
||
35 | const POINT_TYPE_POST_POINT = 2; |
||
36 | const POINT_TYPE_BPACK_247 = 4; |
||
37 | const POINT_TYPE_CLICK_COLLECT_SHOP = 8; |
||
38 | |||
39 | /** @var ApiCaller */ |
||
40 | private $apiCaller; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $appId; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $partner; |
||
51 | |||
52 | /** |
||
53 | * The timeout |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | private $timeOut = 10; |
||
58 | |||
59 | /** |
||
60 | * The user agent |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | private $userAgent; |
||
65 | |||
66 | /** |
||
67 | * Constructor |
||
68 | * |
||
69 | * @param string $partner Static parameter used for protection/statistics |
||
70 | * @param string $appId Static parameter used for protection/statistics |
||
71 | */ |
||
72 | public function __construct($partner, $appId) |
||
73 | { |
||
74 | $this->setPartner((string) $partner); |
||
75 | $this->setAppId((string) $appId); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return ApiCaller |
||
80 | */ |
||
81 | public function getApiCaller() |
||
82 | { |
||
83 | if ($this->apiCaller === null) { |
||
84 | $this->apiCaller = new ApiCaller(new Logger()); |
||
85 | } |
||
86 | |||
87 | return $this->apiCaller; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param ApiCaller $apiCaller |
||
92 | */ |
||
93 | public function setApiCaller(ApiCaller $apiCaller) |
||
94 | { |
||
95 | $this->apiCaller = $apiCaller; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Build the url to be called |
||
100 | * |
||
101 | * @param string $method |
||
102 | * @param array $parameters |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | private function buildUrl($method, array $parameters = array()) |
||
107 | { |
||
108 | return self::API_URL . '?' . $this->buildParameters($method, $parameters); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Build the parameters to send (URL-encoded string) |
||
113 | * |
||
114 | * @param string $method |
||
115 | * @param array $parameters |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | private function buildParameters($method, array $parameters = array()) |
||
120 | { |
||
121 | // add credentials |
||
122 | $parameters['Function'] = $method; |
||
123 | $parameters['Partner'] = $this->getPartner(); |
||
124 | $parameters['AppId'] = $this->getAppId(); |
||
125 | $parameters['Format'] = 'xml'; |
||
126 | |||
127 | return http_build_query($parameters); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Make the real call |
||
132 | * |
||
133 | * @param string $method |
||
134 | * @param array $parameters |
||
135 | * |
||
136 | * @return SimpleXMLElement |
||
137 | * |
||
138 | * @throws BpostCurlException |
||
139 | * @throws BpostInvalidXmlResponseException |
||
140 | * @throws BpostTaxipostLocatorException |
||
141 | */ |
||
142 | private function doCall($method, array $parameters = array()) |
||
143 | { |
||
144 | $options = array( |
||
145 | CURLOPT_URL => self::API_URL, |
||
146 | CURLOPT_USERAGENT => $this->getUserAgent(), |
||
147 | CURLOPT_FOLLOWLOCATION => true, |
||
148 | CURLOPT_SSL_VERIFYPEER => false, |
||
149 | CURLOPT_SSL_VERIFYHOST => false, |
||
150 | CURLOPT_RETURNTRANSFER => true, |
||
151 | CURLOPT_TIMEOUT => (int) $this->getTimeOut(), |
||
152 | |||
153 | CURLOPT_POST => true, |
||
154 | CURLOPT_POSTFIELDS => $this->buildParameters($method, $parameters), |
||
155 | ); |
||
156 | |||
157 | $this->getApiCaller()->doCall($options); |
||
158 | |||
159 | // we expect XML so decode it |
||
160 | $xml = @simplexml_load_string($this->getApiCaller()->getResponseBody()); |
||
161 | |||
162 | // validate xml |
||
163 | if ($xml === false || (isset($xml->head) && isset($xml->body))) { |
||
164 | throw new BpostInvalidXmlResponseException(); |
||
165 | } |
||
166 | |||
167 | // catch generic errors |
||
168 | if (isset($xml['type']) && (string) $xml['type'] == 'TaxipostLocatorError') { |
||
169 | throw new BpostTaxipostLocatorException((string) $xml->txt, (int) $xml->status); |
||
170 | } |
||
171 | |||
172 | // return |
||
173 | return $xml; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param string $appId |
||
178 | */ |
||
179 | public function setAppId($appId) |
||
180 | { |
||
181 | $this->appId = $appId; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | public function getAppId() |
||
188 | { |
||
189 | return $this->appId; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param string $partner |
||
194 | */ |
||
195 | public function setPartner($partner) |
||
196 | { |
||
197 | $this->partner = $partner; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @return string |
||
202 | */ |
||
203 | public function getPartner() |
||
204 | { |
||
205 | return $this->partner; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Set the timeout |
||
210 | * After this time the request will stop. You should handle any errors triggered by this. |
||
211 | * |
||
212 | * @param int $seconds The timeout in seconds. |
||
213 | */ |
||
214 | public function setTimeOut($seconds) |
||
215 | { |
||
216 | $this->timeOut = (int) $seconds; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Get the timeout that will be used |
||
221 | * |
||
222 | * @return int |
||
223 | */ |
||
224 | public function getTimeOut() |
||
225 | { |
||
226 | return (int) $this->timeOut; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get the useragent that will be used. |
||
231 | * Our version will be prepended to yours. |
||
232 | * It will look like: "PHP Bpost/<version> <your-user-agent>" |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function getUserAgent() |
||
237 | { |
||
238 | return (string) 'PHP Bpost Geo6/' . self::VERSION . ' ' . $this->userAgent; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Set the user-agent for you application |
||
243 | * It will be appended to ours, the result will look like: "PHP Bpost/<version> <your-user-agent>" |
||
244 | * |
||
245 | * @param string $userAgent Your user-agent, it should look like <app-name>/<app-version>. |
||
246 | */ |
||
247 | public function setUserAgent($userAgent) |
||
248 | { |
||
249 | $this->userAgent = (string) $userAgent; |
||
250 | } |
||
251 | |||
252 | // webservice methods |
||
253 | /** |
||
254 | * The GetNearestServicePoints web service delivers the nearest bpost pick-up points to a location |
||
255 | * |
||
256 | * @param string $street Street name |
||
257 | * @param string $number Street number |
||
258 | * @param string $zone Postal code and/or city |
||
259 | * @param string $language Language, possible values are: nl, fr |
||
260 | * @param int $type Requested point type, possible values are: |
||
261 | * - 1: Post Office |
||
262 | * - 2: Post Point |
||
263 | * - 3: (1+2, Post Office + Post Point) |
||
264 | * - 4: bpack 24/7 |
||
265 | * - 7: (1+2+4, Post Office + Post Point + bpack 24/7) |
||
266 | * @param int $limit |
||
267 | * @param string $country Country: "BE", "FR"... |
||
268 | * |
||
269 | * @return array |
||
270 | * |
||
271 | * @throws BpostCurlException |
||
272 | * @throws BpostInvalidXmlResponseException |
||
273 | * @throws BpostTaxipostLocatorException |
||
274 | */ |
||
275 | // public function getNearestServicePoint($street, $number, $zone, $country = 'BE', $language = 'nl', $type = 3, $limit = 10) |
||
276 | public function getNearestServicePoint($street, $number, $zone, $language = 'nl', $type = 3, $limit = 10, $country = 'BE') |
||
277 | { |
||
278 | $parameters = array( |
||
279 | 'Street' => (string) $street, |
||
280 | 'Number' => (string) $number, |
||
281 | 'Zone' => (string) $zone, |
||
282 | 'Country' => (string) $country, |
||
283 | 'Language' => (string) $language, |
||
284 | 'Type' => (int) $type, |
||
285 | 'Limit' => (int) $limit, |
||
286 | ); |
||
287 | |||
288 | $xml = $this->doCall('search', $parameters); |
||
289 | |||
290 | if (!isset($xml->PoiList->Poi)) { |
||
291 | throw new BpostInvalidXmlResponseException(); |
||
292 | } |
||
293 | |||
294 | $pois = array(); |
||
295 | foreach ($xml->PoiList->Poi as $poi) { |
||
296 | $pois[] = array( |
||
297 | 'poi' => Poi::createFromXML($poi), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
298 | 'distance' => (float) $poi->Distance, |
||
299 | ); |
||
300 | } |
||
301 | |||
302 | return $pois; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * The GetServicePointDetails web service delivers the details for a bpost |
||
307 | * pick up point referred to by its identifier. |
||
308 | * |
||
309 | * @param string $id Requested point identifier |
||
310 | * @param string $language Language, possible values: nl, fr |
||
311 | * @param int $type Requested point type, possible values are: |
||
312 | * - 1: Post Office |
||
313 | * - 2: Post Point |
||
314 | * - 4: bpack 24/7 |
||
315 | * @param string $country Country: "BE", "FR"... |
||
316 | * |
||
317 | * @return Poi |
||
318 | * |
||
319 | * @throws BpostCurlException |
||
320 | * @throws BpostInvalidXmlResponseException |
||
321 | * @throws BpostTaxipostLocatorException |
||
322 | */ |
||
323 | public function getServicePointDetails($id, $language = 'nl', $type = 3, $country = 'BE') |
||
324 | { |
||
325 | $parameters = array( |
||
326 | 'Id' => (string) $id, |
||
327 | 'Language' => (string) $language, |
||
328 | 'Type' => (int) $type, |
||
329 | 'Country' => (string) $country, |
||
330 | ); |
||
331 | |||
332 | $xml = $this->doCall('info', $parameters); |
||
333 | |||
334 | if (!isset($xml->Poi)) { |
||
335 | throw new BpostInvalidXmlResponseException(); |
||
336 | } |
||
337 | |||
338 | return Poi::createFromXML($xml->Poi); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param int $id |
||
343 | * @param string $language |
||
344 | * @param int $type |
||
345 | * @param string $country |
||
346 | * |
||
347 | * @return string |
||
348 | * |
||
349 | * @see getPointType to feed the param $type |
||
350 | */ |
||
351 | public function getServicePointPageUrl($id, $language = 'nl', $type = 3, $country = 'BE') |
||
352 | { |
||
353 | $parameters = array( |
||
354 | 'Id' => (string) $id, |
||
355 | 'Language' => (string) $language, |
||
356 | 'Type' => (int) $type, |
||
357 | 'Country' => (string) $country, |
||
358 | ); |
||
359 | |||
360 | return $this->buildUrl('page', $parameters); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @param int $id |
||
365 | * @param string $language |
||
366 | * @param int $type |
||
367 | * @param string $country |
||
368 | * |
||
369 | * @return string |
||
370 | * |
||
371 | * @deprecated Renamed |
||
372 | * @see getServicePointPageUrl |
||
373 | */ |
||
374 | public function getServicePointPage($id, $language = 'nl', $type = 3, $country = 'BE') |
||
375 | { |
||
376 | return $this->getServicePointPageUrl($id, $language, $type, $country); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @param bool $withPostOffice |
||
381 | * @param bool $withPostPoint |
||
382 | * @param bool $withBpack247 |
||
383 | * @param bool $withClickAndCollectShop |
||
384 | * |
||
385 | * @return int |
||
386 | */ |
||
387 | public function getPointType( |
||
388 | $withPostOffice = true, |
||
389 | $withPostPoint = true, |
||
390 | $withBpack247 = false, |
||
391 | $withClickAndCollectShop = false |
||
392 | ) { |
||
393 | return |
||
394 | ($withPostOffice ? self::POINT_TYPE_POST_OFFICE : 0) |
||
395 | + ($withPostPoint ? self::POINT_TYPE_POST_POINT : 0) |
||
396 | + ($withBpack247 ? self::POINT_TYPE_BPACK_247 : 0) |
||
397 | + ($withClickAndCollectShop ? self::POINT_TYPE_CLICK_COLLECT_SHOP : 0); |
||
398 | } |
||
399 | } |
||
400 |