简体   繁体   中英

Add xmlns value to Symfony2 SoapServer response

We have built a SOAP Service to receive a request however the response given needs to include an additional value and we don't know how to add it.

We need to add xmlns="http://some-url.co.uk/" into this: <SOAP-ENV:PrintLocationResponse> so that it reads like this <SOAP-ENV:PrintLocationResponse xmlns="http://some-url.co.uk/">

The SOAP Service receives this request:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <PrintLocation xmlns="http://some-url.co.uk/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <alertMsg>
                [ some message goes here]
            </alertMsg>
        </PrintLocation>
    </s:Body>
</s:Envelope>

The SOAP Service responds with this:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <SOAP-ENV:PrintLocationResponse>
            <PrintLocationResult>
                Message contents goes here
            </PrintLocationResult>
        </SOAP-ENV:PrintLocationResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

We need to add xmlns="http://some-url.co.uk/" to this part of the response: <SOAP-ENV:PrintLocationResponse> so that the response will be like this:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <SOAP-ENV:PrintLocationResponse xmlns="http://some-url.co.uk/">
            <PrintLocationResult>
                Message contents goes here
            </PrintLocationResult>
        </SOAP-ENV:PrintLocationResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

We do not know how to add xmlns="http://some-url.co.uk/" into the above response.

The controller managing the request and returning the response is as follows:

    public function indexAction(Request $request)
    {

        $soapServer = new \SoapServer('wsdl/hello.wsdl');
        $soapServer->setObject($this->get('hello_service'));

        $response = new Response();
        $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');

        ob_start();
        $soapServer->handle();

        return $response;

    }

This $soapServer = new \\SoapServer('wsdl/hello.wsdl'); calls the hello.wsdl which looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<wsdl:definitions
             xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
             >

    <wsdl:message name="helloIn">
        <part name="alertMsg" type="xsd:string" />
    </wsdl:message>

    <wsdl:message name="helloOut">
        <part name="PrintLocationResult" type="xsd:string" />
    </wsdl:message>

    <wsdl:portType name="hellowsdlPortType">
        <wsdl:operation name="PrintLocation">
            <wsdl:input message="tns:helloIn"/>
            <wsdl:output message="tns:helloOut"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="hellowsdlBinding" type="tns:hellowsdlPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="PrintLocation">

            <soap:operation soapAction="http://some-url.co.uk/PrintLocation" style="rpc"/>

            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>

        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="hellowsdl">
        <wsdl:port name="hellowsdlPort" binding="tns:hellowsdlBinding">
            <soap:address location="http://backoffice.system/soap" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

The above WSDL is a bit of a mystery for us. We amended the example found here: https://symfony.com/doc/2.7/controller/soap_web_service.html

This $soapServer->setObject($this->get('hello_service')); calls HelloService.php which looks like this:

class HelloService
{
    /**
     * @var EntityManager
     */
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function PrintLocation($msg)
    {
        $log = new TempIressMsgThree($msg);
        $xml = simplexml_load_string($log->getMsg());
        $json = json_encode($xml);
        $array = json_decode($json,TRUE);

        $xml='<?xml version="1.0" encoding="UTF-8"?>
                <message xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.origoservices.com">
                    <m_control>
                       Response message contents goes here.
                    </m_control>
                </message>';

        return $xml;
    }

Can anyone help?

To clarify, we need to add xmlns="http://some-url.co.uk/" into this: <SOAP-ENV:PrintLocationResponse> so that it reads like this <SOAP-ENV:PrintLocationResponse xmlns="http://some-url.co.uk/">

header("Content-type: text/xml");

$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:PrintLocationResponse>
<PrintLocationResult>
Message contents goes here
</PrintLocationResult>
</SOAP-ENV:PrintLocationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;

$content = simplexml_load_string($string,'SimpleXMLElement',LIBXML_NONET);

foreach($content->getDocNamespaces(TRUE) as $shortcut=>$namespace){
    $content->registerXPathNamespace($shortcut,$namespace );
}

$PLR = $content[0]->xpath('//SOAP-ENV:PrintLocationResponse');

foreach($PLR as $response){
    $response->addAttribute('xmlns', 'http://some-url.co.uk/');
}

echo $content[0]->asXML();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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