1

I'm building a web service with soapserver and in the response it shows me the namespace definitions in the envelope section, but the person consuming the web service says that the namespace definition must come in a specific tag.

How can I change where the namespace definition is displayed?

Ex: Currently I get something like this

SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/responsens">
   <SOAP-ENV:Body>
      <ns1:ResponseWS>
         <ns1:childLabel>
            <ns1:labelOne>XXXXXXXXXXX</ns1:labelOne>
            <ns1:labelTwo>XXXXXXX</ns1:labelTwo>
         </ns1:childLabel>
      </ns1:ResponseWS>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

And I want to get something like this

SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <ns1:ResponseWS xmlns:ns1="http://example.com/responsens">
         <ns1:childLabel>
            <ns1:labelOne>XXXXXXXXXXX</ns1:labelOne>
            <ns1:labelTwo>XXXXXXX</ns1:labelTwo>
         </ns1:childLabel>
      </ns1:ResponseWS>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I have tried to modify the wsdl and also using soapvar in the webservice response, but I have not succeeded in any way.

EDIT SOLUTION Navigating the PHP documentation pages I found what I needed.

I have seen on the Handle method page of the Soapserver class that you can modify the XML of Soapserver's response before sending it back to the client.

To do this, we follow a series of steps:

  1. First, you have to obtain the content of the output buffer with the ob_get_contents() method.
  2. Empty the output buffer with ob_end_Clean();
  3. Replace the content we want, in my case I have done it with the preg_replace() method.
  4. Send the content to the output buffer with echo.

Example:

$server = new SoapServer(...);
$server->setClass($class);

// enable the output buffer.
ob_start();

$server->handle();

// get the content of the buffer.
$soapXml = ob_get_contents();

// empty the content of the buffer (if we don't do it, will show what we had and what we changed).
ob_end_clean();

// Eliminates the Namespace declaration of the envelope tag.
$soapXml = preg_replace('/xmlns:ns1=\"http:\/\/example.com\/responsens\"/', '', $soapXml);

// Add the Namespace declaration to the ResponseWS tag.
$soapXml = preg_replace ('/<ns1:ResponseWS>/', '<ns1:ResponseWS xmlns:ns1="http://example.com/responsens">', $soapXml);

// writes xml output in soap response.
echo $soapXml;
1
  • As far as I remember and understand, functionally these are equivalent, and further, back when I worked with XML I think most people wanted the NS declarations to be placed on the root node because it aided in discovery. You could push back to the person to see if they could explain why it must be this way. Otherwise, can you post your WSDL? Commented Aug 12, 2024 at 13:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.