简体   繁体   English

Magento JSON中的自定义REST API响应

[英]Magento custom REST API response in JSON

For Magento 1.9 I'm working on a module where I defined a custom REST route to retrieve all categories with subcategories. 对于Magento 1.9,我正在使用一个模块,在其中定义了自定义REST路由来检索带有子类别的所有类别。 When I call <MAGE>/api/rest/eoarestapi/categories?type=rest the function _retrieveCollection() from the class Namespace_Restapi_Model_Api2_Category_Rest_Guest_V1 is being called. 当我调用<MAGE>/api/rest/eoarestapi/categories?type=rest ,正在调用类Namespace_Restapi_Model_Api2_Category_Rest_Guest_V1的函数_retrieveCollection() So far so good. 到现在为止还挺好。

Now I am having the problem, that it returns the response in XML only and when I set the header explicitly to Accept: application/json , then I get the error 406 Not Acceptable An appropriate representation of the requested resource /api/rest/products could not be found on this server. 现在,我遇到了问题,它仅以XML格式返回响应,并且当我将标头显式设置为Accept: application/json ,我得到错误406 Not Acceptable 所请求资源的适当表示形式/ api / rest / products在此服务器上找不到。 Available variants: api.php , type application/x-httpd-php 可用的变体:api.php,键入application / x-httpd-php

This seems very strange to me as I can remember having worked with JSON response back in Magento 1.8. 这对我来说似乎很奇怪,因为我记得在Magento 1.8中曾使用过JSON响应。

As a fix I found this and that solution to retrieve JSON which works, but it doesn't seem to be a nice solution as it looks like it disables XML response completely. 作为一个修复,我发现解决方案来检索JSON其作品,但它似乎并没有成为一个很好的解决方案,因为它看起来像它完全禁用XML响应。

Is there a better way to enable JSON output from the REST API in Magento 1.9? 是否有更好的方法来启用Magento 1.9中REST API的JSON输出? Does anybody have some background knowledge on this? 有人对此有一些背景知识吗?

I achieved this by overriding my request model. 我通过重写请求模型实现了这一点。 My Steps are following: 我的步骤如下:

1:Declare new module : Create app/etc/modules/Custom_RestJson.xml 1:声明新模块:创建app / etc / modules / Custom_RestJson.xml

<?xml version="1.0"?>
<config>
<modules>
<Custom_RestJson>
<active>true</active>
<codePool>local</codePool>
</Custom_RestJson>
</modules>
</config>

2. Create /app/code/local/Custom/RestJson/etc/config.xml 2.创建/app/code/local/Custom/RestJson/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
<Custom_RestJson>
<version>1.0.0</version>
</Custom_RestJson>
</modules>
<global>
<models>
<Custom_RestJson>
<class>Custom_RestJson_Model</class>
</Custom_RestJson>
<api2>
<rewrite>
<request>Core_Mage_Api2_Model_Request</request>
</rewrite>
</api2>
</models>
</global>
</config>

3. Create /app/code/local/Custom/RestJson/Model/Api2/Request.php 3.创建/app/code/local/Custom/RestJson/Model/Api2/Request.php

<?php
class Custom_RestJson_Model_Api2_Request extends Mage_Api2_Model_Request{

public function getAcceptTypes()
{
$qualityToTypes = array();
$orderedTypes = array();

foreach (preg_split('/,\s*/', $this->getHeader('Accept')) as $definition) {
$typeWithQ = explode(';', $definition);
$mimeType = trim(array_shift($typeWithQ));

// check MIME type validity
if (!preg_match('~^([0-9a-z*+\-]+)(?:/([0-9a-z*+\-\.]+))?$~i', $mimeType)) {
continue;
}
$quality = '1.0'; // default value for quality

if ($typeWithQ) {
$qAndValue = explode('=', $typeWithQ[0]);

if (2 == count($qAndValue)) {
$quality = $qAndValue[1];
}
}
$qualityToTypes[$quality][$mimeType] = true;
}
krsort($qualityToTypes);

foreach ($qualityToTypes as $typeList) {
$orderedTypes += $typeList;
}

return array_keys(array("application/json" => 1));
}

}

First I defined @return mixed in my API interface 首先,我在API接口中定义了@return

and than in your model I do this 比起我的模型

 $response = ['success' => 'ok', 'message' => 'json format'];
 header('Content-Type: application/json');
 echo json_encode($response); exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM