首页 > 解决方案 > 如何在 Magento 2 中使用 Web API 发送 XML 格式的响应?

问题描述

如果在 magento 2 中使用 web api 有请求,我想以 XML 格式发送响应。我参考了这个文档。目前,我收到 JSON 格式的响应。如何将其转换为 XML?

这是我的代码 -

di.xml-

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

     <preference for="Company\Module\Api\GoodsRequestInterface"
     type="Company\Module\Model\GoodsRequest"/>

    <preference for="Company\Module\Api\Data\GoodsDetailsInterface"
    type="Company\Module\Model\GoodsDetails" />
</config>

webapi.xml-

<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route method="POST" url="/V1/financenow/goods-request">
        <service class="Company\Module\Api\GoodsRequestInterface" method="goodsRequest"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Company\Module\Api\GoodsRequestInterface.php-

<?php 
namespace Company\Module\Api;
 
interface GoodsRequestInterface {


    /**
     * @return \Company\Module\Api\Data\GoodsDetailsInterface
     */
    
    public function goodsRequest();
}

Company\Module\Model\GoodsRequest.php-

<?php

namespace Company\Module\Model;

use \SimpleXMLElement;

use Comapny\Module\Api\GoodsRequestInterface;
use Comapny\Module\Api\Data\GoodsDetailsInterfaceFactory;

class GoodsRequest implements GoodsRequestInterface 
{
    public function __construct(
        GoodsDetailsInterfaceFactory $dataFactory
    )
    {
        $this->dataFactory = $dataFactory;
    }

    public function goodsRequest(){
        $factory = $this->dataFactory->create();

        $xml = file_get_contents('php://input');

        $xml = new SimpleXMLElement($xml);
        $body = $xml->xpath('//s:Body');
        $lineItem = $body[0]->HPXML->Line;

        $factory->setStoreCode(0);
        
        return $factory;
    }
}

Company\Module\Api\Data\GoodsDetailsInterface.php-

<?php

namespace Company\Module\Api\Data;

interface GoodsDetailsInterface{
    /**
     * Get Store Code
     *
     * @return int
     */
    public function getStoreCode();

      /**
     * Set Store Code
     *
     * @param int $id
     * @return $this
     */
    public function setStoreCode($id);
}

Company\Module\Model\GoodsDetails.php-

<?php

namespace Company\Module\Model;

use Company\Module\Api\Data\GoodsDetailsInterface;

class GoodsDetails extends \Magento\Framework\Model\AbstractModel implements GoodsDetailsInterface
{
   const STORE_CODE = 'Store_Code';
   
   public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
    }

    public function getStoreCode()
    {
        return $this->_getData(SELF::STORE_CODE);
    }

    /**
     *
     * @param int $id
     * @return $this
     */
    public function setStoreCode($id)
    {
        return $this->setData(SELF::STORE_CODE, $id);
    }
}

如何将 JSON 响应转换为 XML?我尝试了很多解决方案,但得到了 JSON 响应。

标签: phpxmlmagentomagento2

解决方案


推荐阅读