Pregunta

I am using a webapi observer.

In the model, I am requiring a legacy file that echo a well-formed XML for SOAP communication. However, Magento2 echo the result of the Model method, and it does it after the XML document. The resulting output is a badly formatted XML document.

One way I have tried to tackle that issue is to buffer the output of the legacy file, and return the resulting string. However, the resulting string is escaped by magento2 to become a json.

How to get only the legacy XML well-formatted result (with no extra character printed from Magento itself)?

etc/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 url="/v1/liaison/:serial_code" method="POST">
        <service class="\MyCompany\MyModule\Api\Liaison" method="fromOldModule"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="moduleConfig" type="Magento\Core\Model\Config">
        <arguments>
            <argument name="type" xsi:type="string">system</argument>
        </arguments>
    </virtualType>
    <type name="Magento\Core\Model\App">
        <arguments>
            <argument name="config" xsi:type="object">moduleConfig</argument>
        </arguments>
    </type>
</config>

Api/Liaison.php

namespace MyCompany\MyModule\Api;

class Liaison {
    /**
     * @param string $serial_code
     * @return array le XML est déjà affiché via distant.php, donc on n'a pas besoni d'afficher (returnà n'importe quelle autre information
     */
    public function fromOldModule($serial_code) {
        require __DIR__ . '/../v1/liaison/legacyFile.php'; // <-- This legacy code outputs a well-formated XML document
        return '';
    }
}

Result : (the "" at the very end are the characters printed by Magento that should be removed)

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urn:Liaison" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:enc="http://www.w3.org/2003/05/soap-encoding"><env:Body xmlns:rpc="http://www.w3.org/2003/05/soap-rpc"><ns1:ReceiverResponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding"><rpc:result>return</rpc:result><return xsi:type="xsd:boolean">true</return></ns1:ReceiverResponse></env:Body></env:Envelope>
""

I have tried to return null or an empty sting with no success. I have tried to transform the legacy SOAP return statement into an array so that Magento will use it and print it, but that was not successful.

¿Fue útil?

Solución

Magento will convert the returned result from the methode fromOldModule from the file Api/Liaison.php into json.

One simple hack is to end this method with :

die('string to return');

This will return plain text through the api result.

Before that, any output should be captured. This is the result :

    ob_start();
    require __DIR__ . '/../v1/liaison/legacyFile.php'; // <-- This legacy code outputs a well-formated XML document
    $xml = ob_get_flush();
    die($xml);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top