Skip to content

Commit ba6114d

Browse files
committed
Add optional JSON response format
1 parent b9323fd commit ba6114d

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

PSWebServiceLibrary.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function __construct($url, $key, $debug = true)
9696
*
9797
* @throws PrestaShopWebserviceException if HTTP status code is not 200 or 201
9898
*/
99-
protected function checkStatusCode($request)
99+
protected function checkStatusCode($request, $isJson = false)
100100
{
101101
switch ($request['status_code']) {
102102
case 200:
@@ -127,11 +127,22 @@ protected function checkStatusCode($request)
127127
}
128128

129129
if (!empty($error_message)) {
130-
$response = $this->parseXML($request['response']);
131-
$errors = $response->children()->children();
132-
if ($errors && count($errors) > 0) {
133-
foreach ($errors as $error) {
134-
$error_message.= ' - (Code ' . $error->code . '): ' . $error->message;
130+
if ($isJson) {
131+
$errors = json_decode($request['response'], true);
132+
133+
if ($errors['errors'] && count($errors['errors']) > 0) {
134+
foreach ($errors['errors'] as $error) {
135+
$error_message.= ' - (Code ' . $error['code'] . '): ' . $error['message'];
136+
}
137+
}
138+
} else {
139+
$response = $this->parseXML($request['response']);
140+
$errors = $response->children()->children();
141+
142+
if ($errors && count($errors) > 0) {
143+
foreach ($errors as $error) {
144+
$error_message.= ' - (Code ' . $error->code . '): ' . $error->message;
145+
}
135146
}
136147
}
137148
$error_label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.';
@@ -358,7 +369,7 @@ public function add($options)
358369
*
359370
* @param array $options Array representing resource to get.
360371
*
361-
* @return SimpleXMLElement status_code, response
372+
* @return SimpleXMLElement|string status_code, response
362373
* @throws PrestaShopWebserviceException
363374
*/
364375
public function get($options)
@@ -372,7 +383,7 @@ public function get($options)
372383
$url .= '/' . $options['id'];
373384
}
374385

375-
$params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop', 'schema', 'language', 'date', 'price');
386+
$params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop', 'schema', 'language', 'date', 'price', 'output_format');
376387
foreach ($params as $p) {
377388
foreach ($options as $k => $o) {
378389
if (strpos($k, $p) !== false) {
@@ -389,7 +400,13 @@ public function get($options)
389400

390401
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET'));
391402

392-
$this->checkStatusCode($request);// check the response validity
403+
$isJson = isset($options['output_format']) && $options['output_format'] === 'JSON';
404+
405+
$this->checkStatusCode($request, $isJson);// check the response validity
406+
407+
if ($isJson) {
408+
return $request['response'];
409+
}
393410

394411
return $this->parseXML($request['response']);
395412
}

0 commit comments

Comments
 (0)