prestshop Web服务返回JSON


Prestashop Web service to return JSON

在互联网上广泛搜索后,我确信Prestashop不返回JSON格式的数据,它只返回XML格式(这导致跨域访问问题不幸)。

现在,我正试图将XML(由Prestashop返回)转换为JSON。我想写php代码,可以从web服务的XML和发回JSON。为此,我尝试了许多教程,但徒劳无功。转换后的JSON里面没有值,因此是无用的。我尝试过的方法如下:

http://www.sitepoint.com/php-xml-to-json-proxy/
PHP将XML转换为JSON

XML转换:

<name>
<language id="1" xlink:href="http://localhost/prestashop/api/languages/1">
<![CDATA[ iPod Nano ]]>
</language>
</name>
返回的JSON:

"name":{"language":{"@attributes":{"id":"1"}}}

我不知道历史上是否支持这个,但是…

在Prestashop的最新版本(目前是v1.6.0.9)中,你可以通过在请求查询字符串中添加output_format=JSON来获得任何WebService API请求的JSON响应。

http://example.prestashop.com/api/products?output_format=JSON

使用php pear "XML_Serializer":

{
  "product": {
    "id_category_default": "XXX",
    "id_tax_rules_group": "1",
    "type": "simple",
    "id_shop_default": "1",
    "reference": "XXXXXXXX",
    "price": "XX.XXXXXX",
    "active": "1",
    "redirect_type": "404",
    "id_product_redirected": "0",
    "available_for_order": "1",
    "condition": "new",
    "show_price": "1",
    "indexed": "0",
    "visibility": "both",
    "advanced_stock_management": "0",
    "link_rewrite": {
      "language": {
        "_attributes": {
          "id": "1"
        },
        "_content": "ipod-nano"
      }
    },
    "name": {
      "language": {
        "_attributes": {
          "id": "1"
        },
        "_content": "iPod Nano"
      }
    },
    "associations": {
      "categories": {
        "category": {
          "id": "XXX"
        }
      },
      "stock_availables": {
        "stock_available": {
          "id": "XXXX",
          "id_product_attribute": "X"
        }
      }
    }
  }
}

查看此链接: https://www.prestashop.com/forums/topic/602449-aporte-webservice-prestashop-16-json-request-post-y-put/#elComment_2930631

只添加?output_format=JSON结束url

http://example.com/api/products?output_format=JSON

Prestashop有die(Tools::jsonEncode([Associative Array]))用于ajax返回或只是删除die用于非ajax返回。