Как получить формат данных json из веб-службы prestashop

1

Мне нужно вернуть json-формат данных для запроса клиента. Я использовал prestashop 1.6 и включил веб-службы и создал authkey. Я использую ниже код

<?php
define('DEBUG', true);                                          // Debug mode
define('PS_SHOP_PATH', 'http://www.example.com');       // Root path of your PrestaShop store
define('PS_WS_AUTH_KEY', 'myauthkey');  // Auth key (Get it in your Back Office)
require_once('PSWebServiceLibrary.php');

try
{
    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);

    // Here we set the option array for the Webservice : we want customers resources
    $opt['resource'] = 'categories';

    $opt['output_format'] = 'JSON';

    $opt['ps_method']   = 'GET';

    // Call
    $output = $webService->get($opt);
   echo $output;
}
catch (PrestaShopWebserviceException $e)
{
    // Here we are dealing with errors
    $trace = $e->getTrace();
    if ($trace[0]['args'][0] == 404) echo 'Bad ID';
    else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
    else echo 'Other error';
}
?>

Но он возвращается, как

HTTP RESPONSE HEADER
HTTP/1.1 200 OK
Date: Thu, 21 May 2015 10:37:18 GMT
Server: Apache/2.4.10 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4
Access-Time: 1432204638
X-Powered-By: PrestaShop Webservice
PSWS-Version: 1.6.0.14
Execution-Time: 0.012
Content-Sha1: 39de35d56851e6fb74644c38ffcb34215d63822f
Vary: Accept-Encoding
Cache-Control: max-age=3600
Expires: Thu, 21 May 2015 11:37:18 GMT
Transfer-Encoding: chunked
Content-Type: text/xml;charset=utf-8
RETURN HTTP BODY
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<categories>
<category id="1" xlink:href="http://www.example.com/api/categories/1"/>
<category id="2" xlink:href="http://www.example.com/api/categories/2"/>
<category id="57" xlink:href="http://www.example.com/api/categories/57"/>
<category id="58" xlink:href="http://www.example.com/api/categories/58"/>
</categories>
</prestashop>
object(SimpleXMLElement)#2 (1) { ["categories"]=> object(SimpleXMLElement)#3 (1) { ["category"]=> array(17) { [0]=> object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "1" } } [1]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "2" } } [2]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "57" } } [3]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "58" } } [4]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "59" } } [5]=> object(SimpleXMLElement)#9 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "60" } } [6]=> object(SimpleXMLElement)#10 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "61" } } [7]=> object(SimpleXMLElement)#11 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "62" } } [8]=> object(SimpleXMLElement)#12 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "63" } } [9]=> object(SimpleXMLElement)#13 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "64" } } [10]=> object(SimpleXMLElement)#14 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "65" } } [11]=> object(SimpleXMLElement)#15 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "66" } } [12]=> object(SimpleXMLElement)#16 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "67" } } [13]=> object(SimpleXMLElement)#17 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "68" } } [14]=> object(SimpleXMLElement)#18 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "69" } } [15]=> object(SimpleXMLElement)#19 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "70" } } [16]=> object(SimpleXMLElement)#20 (1) { ["@attributes"]=> array(1) { ["id"]=> string(2) "71" } } } } }

как вернуть только json-выход без HTTP HEADER RESPONSE HEADER и RETURN HTTP BODY.

Теги:
prestashop-1.6

2 ответа

0

я решил эту проблему

public function printDebug($title, $content)
    {
        //echo '<div style="display:table;background:#CCC;font-size:8pt;padding:7px"><h6 style="font-size:9pt;margin:0">'.$title.'</h6><pre>'.htmlentities($content).'</pre></div>';
    }

комментарий вроде этого в PSWebServiceLibrary.php ---------- betty koshy

0

Кажется, вы снова получили simpleXMLElement. Который может быть преобразован в JSON с помощью json_encode(), также посмотрите на этот вопрос: PHP конвертирует XML в JSON

  • 0
    Я уже проверяю с помощью json_encode ($ array), что он возвращает данные в формате json, но мне нужно ответить только на строку json, но он также возвращает заголовок HTTP-резонанса и тексты RETURN HTTP BODY. Как убрать эти вещи и ответить только строкой json
  • 0
    Вы можете попробовать удалить все до ВОЗВРАТА HTTP-ТЕЛА с помощью strstr ( php.net/manual/en/function.strstr.php ), а затем поместить его через json_encode, я думаю ... Вот пример для этого: stackoverflow. ком / вопросы / 7802821 / ...
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню