|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* 2007-2022 PrestaShop SA and Contributors |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is bundled with this package in the file LICENSE.txt. |
|
10
|
|
|
* It is also available through the world-wide-web at this URL: |
|
11
|
|
|
* https://opensource.org/licenses/osl-3.0.php |
|
12
|
|
|
* If you did not receive a copy of the license and are unable to |
|
13
|
|
|
* obtain it through the world-wide-web, please send an email |
|
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
|
15
|
|
|
* |
|
16
|
|
|
* DISCLAIMER |
|
17
|
|
|
* |
|
18
|
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer |
|
19
|
|
|
* versions in the future. If you wish to customize PrestaShop for your |
|
20
|
|
|
* needs please refer to https://www.prestashop.com for more information. |
|
21
|
|
|
* |
|
22
|
|
|
* @author PrestaShop SA <[email protected]> |
|
23
|
|
|
* @copyright 2007-2022 PrestaShop SA |
|
24
|
|
|
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
25
|
|
|
* International Registered Trademark & Property of PrestaShop SA |
|
26
|
|
|
* PrestaShop Webservice Library |
|
27
|
|
|
* @package PrestaShopWebservice |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @package PrestaShopWebservice |
|
32
|
|
|
*/ |
|
33
|
|
|
class PrestaShopWebservice |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var string Shop URL */ |
|
36
|
|
|
protected $url; |
|
37
|
|
|
|
|
38
|
|
|
/** @var string Authentication key */ |
|
39
|
|
|
protected $key; |
|
40
|
|
|
|
|
41
|
|
|
/** @var boolean is debug activated */ |
|
42
|
|
|
protected $debug; |
|
43
|
|
|
|
|
44
|
|
|
/** @var string PS version */ |
|
45
|
|
|
protected $version; |
|
46
|
|
|
|
|
47
|
|
|
/** @var string Minimal version of PrestaShop to use with this library */ |
|
48
|
|
|
const PS_COMPATIBLE_VERSIONS_MIN = '1.4.0.0'; |
|
49
|
|
|
/** @var string Maximal version of PrestaShop to use with this library */ |
|
50
|
|
|
const PS_COMPATIBLE_VERSIONS_MAX = '8.1.1'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* PrestaShopWebservice constructor. Throw an exception when CURL is not installed/activated |
|
54
|
|
|
* <code> |
|
55
|
|
|
* <?php |
|
56
|
|
|
* require_once('./PrestaShopWebservice.php'); |
|
57
|
|
|
* try |
|
58
|
|
|
* { |
|
59
|
|
|
* $ws = new PrestaShopWebservice('https://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false); |
|
60
|
|
|
* // Now we have a webservice object to play with |
|
61
|
|
|
* } |
|
62
|
|
|
* catch (PrestaShopWebserviceException $ex) |
|
63
|
|
|
* { |
|
64
|
|
|
* echo 'Error : '.$ex->getMessage(); |
|
65
|
|
|
* } |
|
66
|
|
|
* ?> |
|
67
|
|
|
* </code> |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $url Root URL for the shop |
|
70
|
|
|
* @param string $key Authentication key |
|
71
|
|
|
* @param mixed $debug Debug mode Activated (true) or deactivated (false) |
|
72
|
|
|
* |
|
73
|
|
|
* @throws PrestaShopWebserviceException if curl is not loaded |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct($url, $key, $debug = true) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!extension_loaded('curl')) { |
|
78
|
|
|
throw new PrestaShopWebserviceException( |
|
79
|
|
|
'Please activate the PHP extension \'curl\' to allow use of PrestaShop webservice library' |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
$this->url = $url; |
|
83
|
|
|
$this->key = $key; |
|
84
|
|
|
$this->debug = $debug; |
|
85
|
|
|
$this->version = 'unknown'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Take the status code and throw an exception if the server didn't return 200 or 201 code |
|
90
|
|
|
* <p>Unique parameter must take : <br><br> |
|
91
|
|
|
* 'status_code' => Status code of an HTTP return<br> |
|
92
|
|
|
* 'response' => CURL response |
|
93
|
|
|
* </p> |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $request Response elements of CURL request |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
* @throws PrestaShopWebserviceException if HTTP status code is not 200 or 201 |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function checkStatusCode($request) |
|
101
|
|
|
{ |
|
102
|
|
|
switch ($request['status_code']) { |
|
103
|
|
|
case 200: |
|
104
|
|
|
case 201: |
|
105
|
|
|
break; |
|
106
|
|
|
case 204: |
|
107
|
|
|
$error_message = 'No content'; |
|
108
|
|
|
break; |
|
109
|
|
|
case 400: |
|
110
|
|
|
$error_message = 'Bad Request'; |
|
111
|
|
|
break; |
|
112
|
|
|
case 401: |
|
113
|
|
|
$error_message = 'Unauthorized'; |
|
114
|
|
|
break; |
|
115
|
|
|
case 404: |
|
116
|
|
|
$error_message = 'Not Found'; |
|
117
|
|
|
break; |
|
118
|
|
|
case 405: |
|
119
|
|
|
$error_message = 'Method Not Allowed'; |
|
120
|
|
|
break; |
|
121
|
|
|
case 500: |
|
122
|
|
|
$error_message = 'Internal Server Error'; |
|
123
|
|
|
break; |
|
124
|
|
|
default: |
|
125
|
|
|
throw new PrestaShopWebserviceException( |
|
126
|
|
|
'This call to PrestaShop Web Services returned an unexpected HTTP status of:' . $request['status_code'] |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if (!empty($error_message)) { |
|
131
|
|
|
$response = $this->parseXML($request['response']); |
|
132
|
|
|
$errors = $response->children()->children(); |
|
133
|
|
|
if ($errors && count($errors) > 0) { |
|
134
|
|
|
foreach ($errors as $error) { |
|
135
|
|
|
$error_message .= ' - (Code ' . $error->code . '): ' . $error->message; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
$error_label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.'; |
|
139
|
|
|
throw new PrestaShopWebserviceException(sprintf($error_label, $request['status_code'], $error_message)); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Provides default parameters for the curl connection(s) |
|
145
|
|
|
* @return array Default parameters for curl connection(s) |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function getCurlDefaultParams() |
|
148
|
|
|
{ |
|
149
|
|
|
$defaultParams = array( |
|
150
|
|
|
CURLOPT_HEADER => true, |
|
151
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
152
|
|
|
CURLINFO_HEADER_OUT => true, |
|
153
|
|
|
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
154
|
|
|
CURLOPT_USERPWD => $this->key . ':', |
|
155
|
|
|
CURLOPT_HTTPHEADER => array('Expect:'), |
|
156
|
|
|
//CURLOPT_SSL_VERIFYPEER => false, // reminder, in dev environment sometimes self-signed certificates are used |
|
157
|
|
|
//CURLOPT_CAINFO => "PATH2CAINFO", // ssl certificate chain checking |
|
158
|
|
|
//CURLOPT_CAPATH => "PATH2CAPATH", |
|
159
|
|
|
); |
|
160
|
|
|
return $defaultParams; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Handles a CURL request to PrestaShop Webservice. Can throw exception. |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $url Resource name |
|
167
|
|
|
* @param mixed $curl_params CURL parameters (sent to curl_set_opt) |
|
168
|
|
|
* |
|
169
|
|
|
* @return array status_code, response, header |
|
170
|
|
|
* |
|
171
|
|
|
* @throws PrestaShopWebserviceException |
|
172
|
|
|
*/ |
|
173
|
|
|
public function executeRequest($url, $curl_params = array()) |
|
174
|
|
|
{ |
|
175
|
|
|
$defaultParams = $this->getCurlDefaultParams(); |
|
176
|
|
|
|
|
177
|
|
|
dol_syslog("curl_init url=" . $url); |
|
178
|
|
|
$session = curl_init($url); |
|
179
|
|
|
|
|
180
|
|
|
$curl_options = array(); |
|
181
|
|
|
foreach ($defaultParams as $defkey => $defval) { |
|
182
|
|
|
if (isset($curl_params[$defkey])) { |
|
183
|
|
|
$curl_options[$defkey] = $curl_params[$defkey]; |
|
184
|
|
|
} else { |
|
185
|
|
|
$curl_options[$defkey] = $defaultParams[$defkey]; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
foreach ($curl_params as $defkey => $defval) { |
|
189
|
|
|
if (!isset($curl_options[$defkey])) { |
|
190
|
|
|
$curl_options[$defkey] = $curl_params[$defkey]; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
dol_syslog("curl curl_options = " . var_export($curl_options, true)); |
|
195
|
|
|
curl_setopt_array($session, $curl_options); |
|
196
|
|
|
$response = curl_exec($session); |
|
197
|
|
|
|
|
198
|
|
|
$index = strpos($response, "\r\n\r\n"); |
|
199
|
|
|
if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') { |
|
200
|
|
|
throw new PrestaShopWebserviceException('Bad HTTP response ' . $response . curl_error($session)); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$header = substr($response, 0, $index); |
|
204
|
|
|
$body = substr($response, $index + 4); |
|
205
|
|
|
|
|
206
|
|
|
$headerArrayTmp = explode("\n", $header); |
|
207
|
|
|
|
|
208
|
|
|
$headerArray = array(); |
|
209
|
|
|
foreach ($headerArrayTmp as &$headerItem) { |
|
210
|
|
|
$tmp = explode(':', $headerItem); |
|
211
|
|
|
$tmp = array_map('trim', $tmp); |
|
212
|
|
|
if (count($tmp) == 2) { |
|
213
|
|
|
$headerArray[$tmp[0]] = $tmp[1]; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
if (array_key_exists('PSWS-Version', $headerArray)) { |
|
218
|
|
|
$this->version = $headerArray['PSWS-Version']; |
|
219
|
|
|
if ( |
|
220
|
|
|
version_compare(PrestaShopWebservice::PS_COMPATIBLE_VERSIONS_MIN, $headerArray['PSWS-Version']) == 1 || |
|
221
|
|
|
version_compare(PrestaShopWebservice::PS_COMPATIBLE_VERSIONS_MAX, $headerArray['PSWS-Version']) == -1 |
|
222
|
|
|
) { |
|
223
|
|
|
throw new PrestaShopWebserviceException( |
|
224
|
|
|
'This library is not compatible with this version of PrestaShop. Please upgrade/downgrade this library' |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
if ($this->debug) { |
|
230
|
|
|
$this->printDebug('HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT)); |
|
231
|
|
|
$this->printDebug('HTTP RESPONSE HEADER', $header); |
|
232
|
|
|
} |
|
233
|
|
|
$status_code = curl_getinfo($session, CURLINFO_HTTP_CODE); |
|
234
|
|
|
if ($status_code === 0) { |
|
235
|
|
|
throw new PrestaShopWebserviceException('CURL Error: ' . curl_error($session)); |
|
236
|
|
|
} |
|
237
|
|
|
curl_close($session); |
|
238
|
|
|
if ($this->debug) { |
|
239
|
|
|
if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST') { |
|
240
|
|
|
$this->printDebug('XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS])); |
|
241
|
|
|
} |
|
242
|
|
|
if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') { |
|
243
|
|
|
$this->printDebug('RETURN HTTP BODY', $body); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
return array('status_code' => $status_code, 'response' => $body, 'header' => $header); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Output debug info |
|
251
|
|
|
* |
|
252
|
|
|
* @param string $title Title |
|
253
|
|
|
* @param string $content Content |
|
254
|
|
|
* @return void |
|
255
|
|
|
*/ |
|
256
|
|
|
public function printDebug($title, $content) |
|
257
|
|
|
{ |
|
258
|
|
|
if (php_sapi_name() == 'cli') { |
|
259
|
|
|
echo $title . PHP_EOL . $content; |
|
260
|
|
|
} else { |
|
261
|
|
|
echo '<div style="display:table;background:#CCC;font-size:8pt;padding:7px"><h6 style="font-size:9pt;margin:0">' |
|
262
|
|
|
. $title |
|
263
|
|
|
. '</h6><pre>' |
|
264
|
|
|
. htmlentities($content) |
|
265
|
|
|
. '</pre></div>'; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Return version |
|
271
|
|
|
* |
|
272
|
|
|
* @return string Version |
|
273
|
|
|
*/ |
|
274
|
|
|
public function getVersion() |
|
275
|
|
|
{ |
|
276
|
|
|
return $this->version; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Load XML from string. Can throw exception |
|
281
|
|
|
* |
|
282
|
|
|
* @param string $response String from a CURL response |
|
283
|
|
|
* |
|
284
|
|
|
* @return SimpleXMLElement status_code, response |
|
285
|
|
|
* @throws PrestaShopWebserviceException |
|
286
|
|
|
*/ |
|
287
|
|
|
protected function parseXML($response) |
|
288
|
|
|
{ |
|
289
|
|
|
if ($response != '') { |
|
290
|
|
|
libxml_clear_errors(); |
|
291
|
|
|
libxml_use_internal_errors(true); |
|
292
|
|
|
if (LIBXML_VERSION < 20900) { |
|
293
|
|
|
// Avoid load of external entities (security problem). |
|
294
|
|
|
// Required only if LIBXML_VERSION < 20900 |
|
295
|
|
|
// @phan-suppress-next-line PhanDeprecatedFunctionInternal |
|
296
|
|
|
libxml_disable_entity_loader(true); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
$xml = simplexml_load_string(trim($response), 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NONET); |
|
300
|
|
|
if (libxml_get_errors()) { |
|
301
|
|
|
$msg = var_export(libxml_get_errors(), true); |
|
302
|
|
|
libxml_clear_errors(); |
|
303
|
|
|
throw new PrestaShopWebserviceException('HTTP XML response is not parsable: ' . $msg); |
|
304
|
|
|
} |
|
305
|
|
|
return $xml; |
|
306
|
|
|
} else { |
|
307
|
|
|
throw new PrestaShopWebserviceException('HTTP response is empty'); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Add (POST) a resource |
|
313
|
|
|
* <p>Unique parameter must take : <br><br> |
|
314
|
|
|
* 'resource' => Resource name<br> |
|
315
|
|
|
* 'postXml' => Full XML string to add resource<br><br> |
|
316
|
|
|
* Examples are given in the tutorial</p> |
|
317
|
|
|
* |
|
318
|
|
|
* @param array $options Options |
|
319
|
|
|
* |
|
320
|
|
|
* @return SimpleXMLElement status_code, response |
|
321
|
|
|
* @throws PrestaShopWebserviceException |
|
322
|
|
|
*/ |
|
323
|
|
|
public function add($options) |
|
324
|
|
|
{ |
|
325
|
|
|
$xml = ''; |
|
326
|
|
|
|
|
327
|
|
|
if (isset($options['resource'], $options['postXml']) || isset($options['url'], $options['postXml'])) { |
|
328
|
|
|
$url = (isset($options['resource']) ? $this->url . '/api/' . $options['resource'] : $options['url']); |
|
329
|
|
|
$xml = $options['postXml']; |
|
330
|
|
|
if (isset($options['id_shop'])) { |
|
331
|
|
|
$url .= '&id_shop=' . $options['id_shop']; |
|
332
|
|
|
} |
|
333
|
|
|
if (isset($options['id_group_shop'])) { |
|
334
|
|
|
$url .= '&id_group_shop=' . $options['id_group_shop']; |
|
335
|
|
|
} |
|
336
|
|
|
} else { |
|
337
|
|
|
throw new PrestaShopWebserviceException('Bad parameters given'); |
|
338
|
|
|
} |
|
339
|
|
|
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $xml)); |
|
340
|
|
|
|
|
341
|
|
|
$this->checkStatusCode($request); |
|
342
|
|
|
return $this->parseXML($request['response']); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Retrieve (GET) a resource |
|
347
|
|
|
* <p>Unique parameter must take : <br><br> |
|
348
|
|
|
* 'url' => Full URL for a GET request of Webservice (ex: https://mystore.com/api/customers/1/)<br> |
|
349
|
|
|
* OR<br> |
|
350
|
|
|
* 'resource' => Resource name,<br> |
|
351
|
|
|
* 'id' => ID of a resource you want to get<br><br> |
|
352
|
|
|
* </p> |
|
353
|
|
|
* <code> |
|
354
|
|
|
* <?php |
|
355
|
|
|
* require_once('./PrestaShopWebservice.php'); |
|
356
|
|
|
* try |
|
357
|
|
|
* { |
|
358
|
|
|
* $ws = new PrestaShopWebservice('https://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false); |
|
359
|
|
|
* $xml = $ws->get(array('resource' => 'orders', 'id' => 1)); |
|
360
|
|
|
* // Here in $xml, a SimpleXMLElement object you can parse |
|
361
|
|
|
* foreach ($xml->children()->children() as $attName => $attValue) |
|
362
|
|
|
* echo $attName.' = '.$attValue.'<br>'; |
|
363
|
|
|
* } |
|
364
|
|
|
* catch (PrestaShopWebserviceException $ex) |
|
365
|
|
|
* { |
|
366
|
|
|
* echo 'Error : '.$ex->getMessage(); |
|
367
|
|
|
* } |
|
368
|
|
|
* ?> |
|
369
|
|
|
* </code> |
|
370
|
|
|
* |
|
371
|
|
|
* @param array $options Array representing resource to get. |
|
372
|
|
|
* |
|
373
|
|
|
* @return SimpleXMLElement status_code, response |
|
374
|
|
|
* @throws PrestaShopWebserviceException |
|
375
|
|
|
*/ |
|
376
|
|
|
public function get($options) |
|
377
|
|
|
{ |
|
378
|
|
|
if (isset($options['url'])) { |
|
379
|
|
|
$url = $options['url']; |
|
380
|
|
|
} elseif (isset($options['resource'])) { |
|
381
|
|
|
$url = $this->url . '/api/' . $options['resource']; |
|
382
|
|
|
$url_params = array(); |
|
383
|
|
|
if (isset($options['id'])) { |
|
384
|
|
|
$url .= '/' . $options['id']; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
$params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop', 'schema', 'language', 'date', 'price'); |
|
388
|
|
|
foreach ($params as $p) { |
|
389
|
|
|
foreach ($options as $k => $o) { |
|
390
|
|
|
if (strpos($k, $p) !== false) { |
|
391
|
|
|
$url_params[$k] = $options[$k]; |
|
392
|
|
|
} |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
if (count($url_params) > 0) { |
|
396
|
|
|
$url .= '?' . http_build_query($url_params); |
|
397
|
|
|
} |
|
398
|
|
|
} else { |
|
399
|
|
|
throw new PrestaShopWebserviceException('Bad parameters given'); |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET')); |
|
403
|
|
|
|
|
404
|
|
|
$this->checkStatusCode($request);// check the response validity |
|
405
|
|
|
|
|
406
|
|
|
return $this->parseXML($request['response']); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* Head method (HEAD) a resource |
|
411
|
|
|
* |
|
412
|
|
|
* @param array $options Array representing resource for head request. |
|
413
|
|
|
* |
|
414
|
|
|
* @return SimpleXMLElement status_code, response |
|
415
|
|
|
* @throws PrestaShopWebserviceException |
|
416
|
|
|
*/ |
|
417
|
|
|
public function head($options) |
|
418
|
|
|
{ |
|
419
|
|
|
if (isset($options['url'])) { |
|
420
|
|
|
$url = $options['url']; |
|
421
|
|
|
} elseif (isset($options['resource'])) { |
|
422
|
|
|
$url = $this->url . '/api/' . $options['resource']; |
|
423
|
|
|
$url_params = array(); |
|
424
|
|
|
if (isset($options['id'])) { |
|
425
|
|
|
$url .= '/' . $options['id']; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
$params = array('filter', 'display', 'sort', 'limit'); |
|
429
|
|
|
foreach ($params as $p) { |
|
430
|
|
|
foreach ($options as $k => $o) { |
|
431
|
|
|
if (strpos($k, $p) !== false) { |
|
432
|
|
|
$url_params[$k] = $options[$k]; |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
} |
|
436
|
|
|
if (count($url_params) > 0) { |
|
437
|
|
|
$url .= '?' . http_build_query($url_params); |
|
438
|
|
|
} |
|
439
|
|
|
} else { |
|
440
|
|
|
throw new PrestaShopWebserviceException('Bad parameters given'); |
|
441
|
|
|
} |
|
442
|
|
|
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_NOBODY => true)); |
|
443
|
|
|
$this->checkStatusCode($request);// check the response validity |
|
444
|
|
|
return $request['header']; |
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
|
|
/** |
|
448
|
|
|
* Edit (PUT) a resource |
|
449
|
|
|
* <p>Unique parameter must take : <br><br> |
|
450
|
|
|
* 'resource' => Resource name ,<br> |
|
451
|
|
|
* 'id' => ID of a resource you want to edit,<br> |
|
452
|
|
|
* 'putXml' => Modified XML string of a resource<br><br> |
|
453
|
|
|
* Examples are given in the tutorial</p> |
|
454
|
|
|
* |
|
455
|
|
|
* @param array $options Array representing resource to edit. |
|
456
|
|
|
* |
|
457
|
|
|
* @return SimpleXMLElement |
|
458
|
|
|
* @throws PrestaShopWebserviceException |
|
459
|
|
|
*/ |
|
460
|
|
|
public function edit($options) |
|
461
|
|
|
{ |
|
462
|
|
|
$xml = ''; |
|
463
|
|
|
if (isset($options['url'])) { |
|
464
|
|
|
$url = $options['url']; |
|
465
|
|
|
} elseif ((isset($options['resource'], $options['id']) || isset($options['url'])) && $options['putXml']) { |
|
466
|
|
|
$url = (isset($options['url']) ? $options['url'] : |
|
467
|
|
|
$this->url . '/api/' . $options['resource'] . '/' . $options['id']); |
|
468
|
|
|
$xml = $options['putXml']; |
|
469
|
|
|
if (isset($options['id_shop'])) { |
|
470
|
|
|
$url .= '&id_shop=' . $options['id_shop']; |
|
471
|
|
|
} |
|
472
|
|
|
if (isset($options['id_group_shop'])) { |
|
473
|
|
|
$url .= '&id_group_shop=' . $options['id_group_shop']; |
|
474
|
|
|
} |
|
475
|
|
|
} else { |
|
476
|
|
|
throw new PrestaShopWebserviceException('Bad parameters given'); |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => $xml)); |
|
480
|
|
|
$this->checkStatusCode($request);// check the response validity |
|
481
|
|
|
return $this->parseXML($request['response']); |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
/** |
|
485
|
|
|
* Delete (DELETE) a resource. |
|
486
|
|
|
* Unique parameter must take : <br><br> |
|
487
|
|
|
* 'resource' => Resource name<br> |
|
488
|
|
|
* 'id' => ID or array which contains IDs of a resource(s) you want to delete<br><br> |
|
489
|
|
|
* <code> |
|
490
|
|
|
* <?php |
|
491
|
|
|
* require_once('./PrestaShopWebservice.php'); |
|
492
|
|
|
* try |
|
493
|
|
|
* { |
|
494
|
|
|
* $ws = new PrestaShopWebservice('https://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false); |
|
495
|
|
|
* $xml = $ws->delete(array('resource' => 'orders', 'id' => 1)); |
|
496
|
|
|
* // Following code will not be executed if an exception is thrown. |
|
497
|
|
|
* echo 'Successfully deleted.'; |
|
498
|
|
|
* } |
|
499
|
|
|
* catch (PrestaShopWebserviceException $ex) |
|
500
|
|
|
* { |
|
501
|
|
|
* echo 'Error : '.$ex->getMessage(); |
|
502
|
|
|
* } |
|
503
|
|
|
* ?> |
|
504
|
|
|
* </code> |
|
505
|
|
|
* |
|
506
|
|
|
* @param array $options Array representing resource to delete. |
|
507
|
|
|
* |
|
508
|
|
|
* @return bool |
|
509
|
|
|
* @throws PrestaShopWebserviceException |
|
510
|
|
|
*/ |
|
511
|
|
|
public function delete($options) |
|
512
|
|
|
{ |
|
513
|
|
|
if (isset($options['url'])) { |
|
514
|
|
|
$url = $options['url']; |
|
515
|
|
|
} elseif (isset($options['resource']) && isset($options['id'])) { |
|
516
|
|
|
$url = (is_array($options['id'])) |
|
517
|
|
|
? $this->url . '/api/' . $options['resource'] . '/?id=[' . implode(',', $options['id']) . ']' |
|
518
|
|
|
: $this->url . '/api/' . $options['resource'] . '/' . $options['id']; |
|
519
|
|
|
} else { |
|
520
|
|
|
throw new PrestaShopWebserviceException('Bad parameters given'); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
if (isset($options['id_shop'])) { |
|
524
|
|
|
$url .= '&id_shop=' . $options['id_shop']; |
|
525
|
|
|
} |
|
526
|
|
|
if (isset($options['id_group_shop'])) { |
|
527
|
|
|
$url .= '&id_group_shop=' . $options['id_group_shop']; |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'DELETE')); |
|
531
|
|
|
$this->checkStatusCode($request);// check the response validity |
|
532
|
|
|
return true; |
|
533
|
|
|
} |
|
534
|
|
|
} |
|
535
|
|
|
|
|
536
|
|
|
/** |
|
537
|
|
|
* @package PrestaShopWebservice |
|
538
|
|
|
*/ |
|
539
|
|
|
class PrestaShopWebserviceException extends Exception |
|
540
|
|
|
{ |
|
541
|
|
|
} |
|
542
|
|
|
|