2015-05-21 83 views
1

我需要为客户端请求返回json格式的数据。我使用了prestashop 1.6并启用了Web服务并创建了authkey。 我使用下面的代码如何从prestashop web服务获取json格式的数据

<?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" } } } } } 

如何在不HTTP响应头并返回HTTP BODY只返回JSON输出。

回答

0

看来你得到了一个simpleXMLElement。可以通过使用json_encode()转换成JSON也看这个问题:PHP convert XML to JSON

+0

我已经使用json_encode($ array)检查它是否返回json格式的数据,但是我只需要响应json字符串,但是它会返回http resonse header和RETURN HTTP BODY文本。如何删除这些东西,并只响应json字符串 –

+0

你可以尝试去掉所有的东西,返回HTTP身体与strstr(http://php.net/manual/en/function.strstr.php),然后把它槽json_encode我猜这里是一个例子,只是做这个:http://stackoverflow.com/questions/7802821/php-remove-all-characters-before-specific-string –

+0

如果我不回应任何东西,它默认显示HTTP RESPONSE HEADER和RETURN HTTP BODY详细信息。为什么prestashop webservice模块回显http内容 –

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 ----------贝蒂koshy