phpzend-frameworksoapsoapuizend-soap

zend soap server response set custom ns1 namespace


I am using Zend_Soap_Server (WSDL mode) for outputting an xml response to the client calls. However, i want to set a custom name for the ns1 namespace in the response.

I noticed that the namespace in the response is set by default like: 'ns1:getDoubleResponse' where 'getDouble' is the server method being called.

Here is my controller and SOAP server setup:

class TestController extends Zend_Controller_Action {

    public function testAction() {

        // diable laoyouts and renderers
        $this->getHelper ( 'viewRenderer' )->setNoRender ( true );
        $server = new Zend_Soap_Server ('http://example.com/public/test/testwsdl'); 
        $server->setClass ( 'Application_Model_test');

        // register exceptions that generate SOAP faults
        $server->registerFaultException('Application_Model_soapException');

        // handle request
        $server->handle ();
    }

    public function testwsdlAction() {
        // diable laoyouts and renderers
        $this->getHelper ( 'viewRenderer' )->setNoRender ( true );      
        $wsdl = new Zend_Soap_AutoDiscover ();

        $wsdl->setClass ( 'Application_Model_test');    
        $wsdl->setUri ('http://example.com/public/test/test');

        // handle request
        $wsdl->handle ();
    }
}

This is my model code:

class Application_Model_test
{
    /**
     * Returns the double of an integer value
     * @param integer $int
     * @return string
     */
    public function getDouble($int)
    {
        $doc = new DOMDocument ( '1.0', 'utf-8' );

        $response = $doc->createElement("IntegerResult");
        $val = $doc->createElement("Value");
        $val->appendChild ($doc->createTextNode($int * 2));     
        $response->appendChild($val);           

        $doc->appendChild ($response);      
        $result = $doc->saveXML();
        return $result;
    }   
}

This is the request i see, as per SOAP UI:

    <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://example.com/public/test/test">
   <soapenv:Header/>
   <soapenv:Body>
      <test:getDouble soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <int xsi:type="xsd:int" xs:type="type:int" xmlns:xs="http://www.w3.org/2000/XMLSchema-instance">3</int>
      </test:getDouble>
   </soapenv:Body>
</soapenv:Envelope>

And this is the associated response, as per SOAP UI:

    <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/public/test/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:getDoubleResponse>
         <return xsi:type="xsd:string">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;IntegerResult>&lt;Value>6&lt;/Value>&lt;/IntegerResult></return>
      </ns1:getDoubleResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I just want to change the <ns1:getDoubleResponse> in SOAP response to something like <ns1:TestResult>

How can i fix namespace? I don't mind throwing the response through DOM or Xpath. I am also interested in extending the Zend_Soap_Server for customizing the response.

UPDATE:

I have extended the Zend_Soap_Server with a class, and now tried to send a custom response through the handle() method.

// TestController.php
//$server = new Zend_Soap_Server ('http://example.com/public/test/testwsdl');
$server = new TestSoapServer ('http://example.com/public/test/testwsdl');

And this is the class that extends Zend_Soap_Server, and handles the response:

// TestController.php
class TestSoapServer extends Zend_Soap_Server 
{
    public function __construct($wsdl, $options = null)
    {
        return parent::__construct($wsdl, $options);
    }

    public function handle($request = null)
    {
      $result = parent::handle($request);
      $result = str_replace("getDoubleResponse", "TestResult", $result);
      return $result;       
    }
}

But now, when i run the request in SOAP UI, i see an empty response. Don't know what i am doing wrong.


Solution

  • Finally, i decided to parse the incoming SOAP request manually in my Controller:

    // TestController.php
    class TestSoapServer extends Zend_Soap_Server 
    {
        // Handle the request and generate suitable response    
        public function handle($request = null)
        {
          if (null === $request) {    
           $request = file_get_contents('php://input');
          }
          // Parse request, generate a static/dynamic response and return it.
    
          // return parent::handle($request); // Actual response
    
        // Custom response
        $doc = new DOMDocument();
        libxml_use_internal_errors(true);
        $doc->loadHTML($request);
        libxml_clear_errors();
        $xml = $doc->saveXML($doc->documentElement);
        $xml = simplexml_load_string($xml);
        $int = $xml->body->envelope->body->getdouble->int;
        $value = $int * 2;
    
        $result = '<SOAP-ENV:Envelope 
                   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"             
                   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
                   xmlns:ns1="http://example.com/public/test/test" 
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
                   <SOAP-ENV:Body>
                   <ns1:TestResult>';
    
         $result .= '<IntegerResult><Value>'.$value.'</Value></IntegerResult>';
    
        $result .= '</ns1:TestResult>
                    </SOAP-ENV:Body>
                    </SOAP-ENV:Envelope>';
    
        return $result;
        }
    }