1
|
|
|
<?php |
2
|
|
|
namespace Bpost\BpostApiClient; |
3
|
|
|
|
4
|
|
|
use Bpost\BpostApiClient\Bpack247\Customer; |
5
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostApiBusinessException; |
6
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostApiSystemException; |
7
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostCurlException; |
8
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostInvalidResponseException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* bPost Bpack24/7 class |
12
|
|
|
* |
13
|
|
|
* @author Tijs Verkoyen <[email protected]> |
14
|
|
|
* @version 3.0.0 |
15
|
|
|
* @copyright Copyright (c), Tijs Verkoyen. All rights reserved. |
16
|
|
|
* @license BSD License |
17
|
|
|
*/ |
18
|
|
|
class Bpack247 |
19
|
|
|
{ |
20
|
|
|
// URL for the api |
21
|
|
|
const API_URL = 'http://www.bpack247.be/BpostRegistrationWebserviceREST/servicecontroller.svc'; |
22
|
|
|
|
23
|
|
|
// current version |
24
|
|
|
const VERSION = '3.0.0'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The account id |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $accountId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A cURL instance |
35
|
|
|
* |
36
|
|
|
* @var resource |
37
|
|
|
*/ |
38
|
|
|
private $curl; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The passPhrase |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $passPhrase; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The port to use. |
49
|
|
|
* |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
private $port; |
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The timeout |
56
|
|
|
* |
57
|
|
|
* @var int |
58
|
|
|
*/ |
59
|
|
|
private $timeOut = 30; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The user agent |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $userAgent; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Make the call |
70
|
|
|
* |
71
|
|
|
* @param string $url The URL to call. |
72
|
|
|
* @param string $body The data to pass. |
|
|
|
|
73
|
|
|
* @param string $method The HTTP-method to use. |
74
|
|
|
* @return \SimpleXMLElement |
75
|
|
|
* @throws BpostApiBusinessException |
76
|
|
|
* @throws BpostApiSystemException |
77
|
|
|
* @throws BpostCurlException |
78
|
|
|
* @throws BpostInvalidResponseException |
79
|
|
|
*/ |
80
|
|
|
private function doCall($url, $body = null, $method = 'GET') |
81
|
|
|
{ |
82
|
|
|
// build Authorization header |
83
|
|
|
$headers[] = 'Authorization: Basic ' . $this->getAuthorizationHeader(); |
|
|
|
|
84
|
|
|
|
85
|
|
|
// set options |
86
|
|
|
$options[CURLOPT_URL] = self::API_URL . $url; |
|
|
|
|
87
|
|
|
$options[CURLOPT_USERAGENT] = $this->getUserAgent(); |
88
|
|
|
$options[CURLOPT_RETURNTRANSFER] = true; |
89
|
|
|
$options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut(); |
90
|
|
|
$options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; |
91
|
|
|
$options[CURLOPT_HTTPHEADER] = $headers; |
92
|
|
|
|
93
|
|
View Code Duplication |
if ($method == 'POST') { |
|
|
|
|
94
|
|
|
$options[CURLOPT_POST] = true; |
95
|
|
|
$options[CURLOPT_POSTFIELDS] = $body; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// init |
99
|
|
|
$this->curl = curl_init(); |
100
|
|
|
|
101
|
|
|
// set options |
102
|
|
|
curl_setopt_array($this->curl, $options); |
103
|
|
|
|
104
|
|
|
// execute |
105
|
|
|
$response = curl_exec($this->curl); |
106
|
|
|
$headers = curl_getinfo($this->curl); |
107
|
|
|
|
108
|
|
|
// fetch errors |
109
|
|
|
$errorNumber = curl_errno($this->curl); |
110
|
|
|
$errorMessage = curl_error($this->curl); |
111
|
|
|
|
112
|
|
|
// error? |
113
|
|
|
if ($errorNumber != '') { |
114
|
|
|
throw new BpostCurlException($errorMessage, $errorNumber); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// valid HTTP-code |
118
|
|
|
if (!in_array($headers['http_code'], array(0, 200))) { |
119
|
|
|
$xml = @simplexml_load_string($response); |
120
|
|
|
|
121
|
|
|
if ( |
122
|
|
|
$xml !== false |
123
|
|
|
&& ($xml->getName() == 'businessException' || $xml->getName() == 'systemException') |
124
|
|
|
) { |
125
|
|
|
$message = (string) $xml->message; |
126
|
|
|
$code = isset($xml->code) ? (int) $xml->code : null; |
127
|
|
|
switch ($xml->getName()) { |
128
|
|
|
case 'businessException': |
129
|
|
|
throw new BpostApiBusinessException($message, $code); |
130
|
|
|
case 'systemException': |
131
|
|
|
throw new BpostApiSystemException($message, $code); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
throw new BpostInvalidResponseException('', $headers['http_code']); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// convert into XML |
139
|
|
|
$xml = simplexml_load_string($response); |
140
|
|
|
|
141
|
|
|
// validate |
142
|
|
|
if ($xml->getName() == 'businessException') { |
143
|
|
|
$message = (string) $xml->message; |
144
|
|
|
$code = (string) $xml->code; |
145
|
|
|
throw new BpostApiBusinessException($message, $code); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// return the response |
149
|
|
|
return $xml; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Generate the secret string for the Authorization header |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
private function getAuthorizationHeader() |
158
|
|
|
{ |
159
|
|
|
return base64_encode($this->accountId . ':' . $this->passPhrase); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the timeout |
164
|
|
|
* After this time the request will stop. You should handle any errors triggered by this. |
165
|
|
|
* |
166
|
|
|
* @param int $seconds The timeout in seconds. |
167
|
|
|
*/ |
168
|
|
|
public function setTimeOut($seconds) |
169
|
|
|
{ |
170
|
|
|
$this->timeOut = (int) $seconds; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the timeout that will be used |
175
|
|
|
* |
176
|
|
|
* @return int |
177
|
|
|
*/ |
178
|
|
|
public function getTimeOut() |
179
|
|
|
{ |
180
|
|
|
return (int) $this->timeOut; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the useragent that will be used. |
185
|
|
|
* Our version will be prepended to yours. |
186
|
|
|
* It will look like: "PHP Bpost/<version> <your-user-agent>" |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function getUserAgent() |
191
|
|
|
{ |
192
|
|
|
return (string) 'PHP Bpost Bpack247/' . self::VERSION . ' ' . $this->userAgent; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set the user-agent for you application |
197
|
|
|
* It will be appended to ours, the result will look like: "PHP Bpost/<version> <your-user-agent>" |
198
|
|
|
* |
199
|
|
|
* @param string $userAgent Your user-agent, it should look like <app-name>/<app-version>. |
200
|
|
|
*/ |
201
|
|
|
public function setUserAgent($userAgent) |
202
|
|
|
{ |
203
|
|
|
$this->userAgent = (string) $userAgent; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Create Bpost instance |
208
|
|
|
* |
209
|
|
|
* @param string $accountId |
210
|
|
|
* @param string $passPhrase |
211
|
|
|
*/ |
212
|
|
|
public function __construct($accountId, $passPhrase) |
213
|
|
|
{ |
214
|
|
|
$this->accountId = (string) $accountId; |
215
|
|
|
$this->passPhrase = (string) $passPhrase; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// webservice methods |
219
|
|
|
/** |
220
|
|
|
* @param Customer $customer |
221
|
|
|
* |
222
|
|
|
* @return \SimpleXMLElement |
223
|
|
|
* @throws BpostApiBusinessException |
224
|
|
|
* @throws BpostApiSystemException |
225
|
|
|
* @throws BpostCurlException |
226
|
|
|
* @throws BpostInvalidResponseException |
227
|
|
|
*/ |
228
|
|
|
public function createMember(Customer $customer) |
229
|
|
|
{ |
230
|
|
|
$url = '/customer'; |
231
|
|
|
|
232
|
|
|
$document = new \DOMDocument('1.0', 'utf-8'); |
233
|
|
|
$document->preserveWhiteSpace = false; |
234
|
|
|
$document->formatOutput = true; |
235
|
|
|
|
236
|
|
|
$document->appendChild( |
237
|
|
|
$customer->toXML( |
238
|
|
|
$document |
239
|
|
|
) |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
return $this->doCall( |
243
|
|
|
$url, |
244
|
|
|
$document->saveXML(), |
245
|
|
|
'POST' |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Retrieve member information |
251
|
|
|
* |
252
|
|
|
* @param string $id |
253
|
|
|
* |
254
|
|
|
* @return Customer |
255
|
|
|
* @throws BpostApiBusinessException |
256
|
|
|
* @throws BpostApiSystemException |
257
|
|
|
* @throws BpostCurlException |
258
|
|
|
* @throws BpostInvalidResponseException |
259
|
|
|
* @throws Exception\XmlException\BpostXmlNoUserIdFoundException |
260
|
|
|
*/ |
261
|
|
|
public function getMember($id) |
262
|
|
|
{ |
263
|
|
|
$xml = $this->doCall( |
264
|
|
|
'/customer/' . $id |
265
|
|
|
); |
266
|
|
|
|
267
|
|
|
return Customer::createFromXML($xml); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.