1
1
<?php
2
- /*
3
- * 2007-2013 PrestaShop
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0)
8
- * that is bundled with this package in the file LICENSE.txt.
9
- * It is also available through the world-wide-web at this URL:
10
- * http://opensource.org/licenses/osl-3.0.php
11
- * If you did not receive a copy of the license and are unable to
12
- * obtain it through the world-wide-web, please send an email
13
- * to [email protected] so we can send you a copy immediately.
14
- *
15
- * DISCLAIMER
16
- *
17
- * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18
- * versions in the future. If you wish to customize PrestaShop for your
19
- * needs please refer to http://www.prestashop.com for more information.
20
- *
21
- * @author PrestaShop SA <[email protected] >
22
- * @copyright 2007-2013 PrestaShop SA
23
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
24
- * International Registered Trademark & Property of PrestaShop SA
25
- * PrestaShop Webservice Library
26
- * @package PrestaShopWebservice
27
- */
28
2
29
3
namespace Protechstudio \PrestashopWebService ;
4
+ use SimpleXMLElement ;
30
5
31
6
/**
32
7
* @package PrestaShopWebservice
@@ -50,26 +25,27 @@ class PrestashopWebServiceLibrary
50
25
const psCompatibleVersionsMin = '1.4.0.0 ' ;
51
26
const psCompatibleVersionsMax = '1.7.99.99 ' ;
52
27
53
- /**
54
- * PrestaShopWebservice constructor. Throw an exception when CURL is not installed/activated
55
- * <code>
56
- * <?php
57
- * require_once('./PrestaShopWebservice.php');
58
- * try
59
- * {
60
- * $ws = new PrestaShopWebservice('http://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false);
61
- * // Now we have a webservice object to play with
62
- * }
63
- * catch (PrestaShopWebserviceException $ex)
64
- * {
65
- * echo 'Error : '.$ex->getMessage();
66
- * }
67
- * ?>
68
- * </code>
69
- * @param string $url Root URL for the shop
70
- * @param string $key Authentification key
71
- * @param mixed $debug Debug mode Activated (true) or deactivated (false)
72
- */
28
+ /**
29
+ * PrestaShopWebservice constructor. Throw an exception when CURL is not installed/activated
30
+ * <code>
31
+ * <?php
32
+ * require_once('./PrestaShopWebservice.php');
33
+ * try
34
+ * {
35
+ * $ws = new PrestaShopWebservice('http://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false);
36
+ * // Now we have a webservice object to play with
37
+ * }
38
+ * catch (PrestaShopWebserviceException $ex)
39
+ * {
40
+ * echo 'Error : '.$ex->getMessage();
41
+ * }
42
+ * ?>
43
+ * </code>
44
+ * @param string $url Root URL for the shop
45
+ * @param string $key Authentification key
46
+ * @param mixed $debug Debug mode Activated (true) or deactivated (false)
47
+ * @throws PrestaShopWebserviceException
48
+ */
73
49
function __construct ($ url , $ key , $ debug = true ) {
74
50
if (!extension_loaded ('curl ' ))
75
51
throw new PrestaShopWebserviceException ('Please activate the PHP extension \'curl \' to allow use of PrestaShop webservice library ' );
@@ -79,10 +55,11 @@ function __construct($url, $key, $debug = true) {
79
55
$ this ->version = 'unknown ' ;
80
56
}
81
57
82
- /**
83
- * Take the status code and throw an exception if the server didn't return 200 or 201 code
84
- * @param int $status_code Status code of an HTTP return
85
- */
58
+ /**
59
+ * Take the status code and throw an exception if the server didn't return 200 or 201 code
60
+ * @param int $status_code Status code of an HTTP return
61
+ * @throws PrestaShopWebserviceException
62
+ */
86
63
protected function checkStatusCode ($ status_code )
87
64
{
88
65
$ error_label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s. ' ;
@@ -98,12 +75,14 @@ protected function checkStatusCode($status_code)
98
75
default : throw new PrestaShopWebserviceException ('This call to PrestaShop Web Services returned an unexpected HTTP status of: ' . $ status_code );
99
76
}
100
77
}
101
- /**
102
- * Handles a CURL request to PrestaShop Webservice. Can throw exception.
103
- * @param string $url Resource name
104
- * @param mixed $curl_params CURL parameters (sent to curl_set_opt)
105
- * @return array status_code, response
106
- */
78
+
79
+ /**
80
+ * Handles a CURL request to PrestaShop Webservice. Can throw exception.
81
+ * @param string $url Resource name
82
+ * @param mixed $curl_params CURL parameters (sent to curl_set_opt)
83
+ * @return array status_code, response
84
+ * @throws PrestaShopWebserviceException
85
+ */
107
86
protected function executeRequest ($ url , $ curl_params = array ())
108
87
{
109
88
$ defaultParams = array (
@@ -190,11 +169,12 @@ public function getVersion()
190
169
return $ this ->version ;
191
170
}
192
171
193
- /**
194
- * Load XML from string. Can throw exception
195
- * @param string $response String from a CURL response
196
- * @return SimpleXMLElement status_code, response
197
- */
172
+ /**
173
+ * Load XML from string. Can throw exception
174
+ * @param string $response String from a CURL response
175
+ * @return SimpleXMLElement status_code, response
176
+ * @throws PrestaShopWebserviceException
177
+ */
198
178
protected function parseXML ($ response )
199
179
{
200
180
if ($ response != '' )
@@ -214,15 +194,16 @@ protected function parseXML($response)
214
194
throw new PrestaShopWebserviceException ('HTTP response is empty ' );
215
195
}
216
196
217
- /**
218
- * Add (POST) a resource
219
- * <p>Unique parameter must take : <br><br>
220
- * 'resource' => Resource name<br>
221
- * 'postXml' => Full XML string to add resource<br><br>
222
- * Examples are given in the tutorial</p>
223
- * @param array $options
224
- * @return SimpleXMLElement status_code, response
225
- */
197
+ /**
198
+ * Add (POST) a resource
199
+ * <p>Unique parameter must take : <br><br>
200
+ * 'resource' => Resource name<br>
201
+ * 'postXml' => Full XML string to add resource<br><br>
202
+ * Examples are given in the tutorial</p>
203
+ * @param array $options
204
+ * @return SimpleXMLElement status_code, response
205
+ * @throws PrestaShopWebserviceException
206
+ */
226
207
public function add ($ options )
227
208
{
228
209
$ xml = '' ;
@@ -244,34 +225,35 @@ public function add($options)
244
225
return self ::parseXML ($ request ['response ' ]);
245
226
}
246
227
247
- /**
248
- * Retrieve (GET) a resource
249
- * <p>Unique parameter must take : <br><br>
250
- * 'url' => Full URL for a GET request of Webservice (ex: http://mystore.com/api/customers/1/)<br>
251
- * OR<br>
252
- * 'resource' => Resource name,<br>
253
- * 'id' => ID of a resource you want to get<br><br>
254
- * </p>
255
- * <code>
256
- * <?php
257
- * require_once('./PrestaShopWebservice.php');
258
- * try
259
- * {
260
- * $ws = new PrestaShopWebservice('http://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false);
261
- * $xml = $ws->get(array('resource' => 'orders', 'id' => 1));
262
- * // Here in $xml, a SimpleXMLElement object you can parse
263
- * foreach ($xml->children()->children() as $attName => $attValue)
264
- * echo $attName.' = '.$attValue.'<br />';
265
- * }
266
- * catch (PrestaShopWebserviceException $ex)
267
- * {
268
- * echo 'Error : '.$ex->getMessage();
269
- * }
270
- * ?>
271
- * </code>
272
- * @param array $options Array representing resource to get.
273
- * @return SimpleXMLElement status_code, response
274
- */
228
+ /**
229
+ * Retrieve (GET) a resource
230
+ * <p>Unique parameter must take : <br><br>
231
+ * 'url' => Full URL for a GET request of Webservice (ex: http://mystore.com/api/customers/1/)<br>
232
+ * OR<br>
233
+ * 'resource' => Resource name,<br>
234
+ * 'id' => ID of a resource you want to get<br><br>
235
+ * </p>
236
+ * <code>
237
+ * <?php
238
+ * require_once('./PrestaShopWebservice.php');
239
+ * try
240
+ * {
241
+ * $ws = new PrestaShopWebservice('http://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false);
242
+ * $xml = $ws->get(array('resource' => 'orders', 'id' => 1));
243
+ * // Here in $xml, a SimpleXMLElement object you can parse
244
+ * foreach ($xml->children()->children() as $attName => $attValue)
245
+ * echo $attName.' = '.$attValue.'<br />';
246
+ * }
247
+ * catch (PrestaShopWebserviceException $ex)
248
+ * {
249
+ * echo 'Error : '.$ex->getMessage();
250
+ * }
251
+ * ?>
252
+ * </code>
253
+ * @param array $options Array representing resource to get.
254
+ * @return SimpleXMLElement status_code, response
255
+ * @throws PrestaShopWebserviceException
256
+ */
275
257
public function get ($ options )
276
258
{
277
259
if (isset ($ options ['url ' ]))
@@ -300,12 +282,13 @@ public function get($options)
300
282
return self ::parseXML ($ request ['response ' ]);
301
283
}
302
284
303
- /**
304
- * Head method (HEAD) a resource
305
- *
306
- * @param array $options Array representing resource for head request.
307
- * @return SimpleXMLElement status_code, response
308
- */
285
+ /**
286
+ * Head method (HEAD) a resource
287
+ *
288
+ * @param array $options Array representing resource for head request.
289
+ * @return SimpleXMLElement status_code, response
290
+ * @throws PrestaShopWebserviceException
291
+ */
309
292
public function head ($ options )
310
293
{
311
294
if (isset ($ options ['url ' ]))
@@ -331,15 +314,18 @@ public function head($options)
331
314
self ::checkStatusCode ($ request ['status_code ' ]);// check the response validity
332
315
return $ request ['header ' ];
333
316
}
334
- /**
335
- * Edit (PUT) a resource
336
- * <p>Unique parameter must take : <br><br>
337
- * 'resource' => Resource name ,<br>
338
- * 'id' => ID of a resource you want to edit,<br>
339
- * 'putXml' => Modified XML string of a resource<br><br>
340
- * Examples are given in the tutorial</p>
341
- * @param array $options Array representing resource to edit.
342
- */
317
+
318
+ /**
319
+ * Edit (PUT) a resource
320
+ * <p>Unique parameter must take : <br><br>
321
+ * 'resource' => Resource name ,<br>
322
+ * 'id' => ID of a resource you want to edit,<br>
323
+ * 'putXml' => Modified XML string of a resource<br><br>
324
+ * Examples are given in the tutorial</p>
325
+ * @param array $options Array representing resource to edit.
326
+ * @return SimpleXMLElement
327
+ * @throws PrestaShopWebserviceException
328
+ */
343
329
public function edit ($ options )
344
330
{
345
331
$ xml = '' ;
@@ -362,29 +348,30 @@ public function edit($options)
362
348
return self ::parseXML ($ request ['response ' ]);
363
349
}
364
350
365
- /**
366
- * Delete (DELETE) a resource.
367
- * Unique parameter must take : <br><br>
368
- * 'resource' => Resource name<br>
369
- * 'id' => ID or array which contains IDs of a resource(s) you want to delete<br><br>
370
- * <code>
371
- * <?php
372
- * require_once('./PrestaShopWebservice.php');
373
- * try
374
- * {
375
- * $ws = new PrestaShopWebservice('http://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false);
376
- * $xml = $ws->delete(array('resource' => 'orders', 'id' => 1));
377
- * // Following code will not be executed if an exception is thrown.
378
- * echo 'Successfully deleted.';
379
- * }
380
- * catch (PrestaShopWebserviceException $ex)
381
- * {
382
- * echo 'Error : '.$ex->getMessage();
383
- * }
384
- * ?>
385
- * </code>
386
- * @param array $options Array representing resource to delete.
387
- */
351
+ /**
352
+ * Delete (DELETE) a resource.
353
+ * Unique parameter must take : <br><br>
354
+ * 'resource' => Resource name<br>
355
+ * 'id' => ID or array which contains IDs of a resource(s) you want to delete<br><br>
356
+ * <code>
357
+ * <?php
358
+ * require_once('./PrestaShopWebservice.php');
359
+ * try
360
+ * {
361
+ * $ws = new PrestaShopWebservice('http://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false);
362
+ * $xml = $ws->delete(array('resource' => 'orders', 'id' => 1));
363
+ * // Following code will not be executed if an exception is thrown.
364
+ * echo 'Successfully deleted.';
365
+ * }
366
+ * catch (PrestaShopWebserviceException $ex)
367
+ * {
368
+ * echo 'Error : '.$ex->getMessage();
369
+ * }
370
+ * ?>
371
+ * </code>
372
+ * @param array $options Array representing resource to delete.
373
+ * @return bool
374
+ */
388
375
public function delete ($ options )
389
376
{
390
377
if (isset ($ options ['url ' ]))
@@ -404,9 +391,4 @@ public function delete($options)
404
391
}
405
392
406
393
407
- }
408
-
409
- /**
410
- * @package PrestaShopWebservice
411
- */
412
- class PrestaShopWebserviceException extends \Exception { }
394
+ }
0 commit comments