zend soap服务器响应设置自定义ns1命名空间

时间:2013-12-30 09:40:23

标签: php zend-framework soap soapui zend-soap

我使用Zend_Soap_Server(WSDL模式)输出对客户端调用的xml响应。 但是,我想在响应中为 ns1 命名空间设置自定义名称。

我注意到响应中的命名空间默认设置为:' ns1:getDoubleResponse '其中' getDouble '是被调用的服务器方法。

这是我的控制器和SOAP服务器设置:

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 ();
    }
}

这是我的型号代码:

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;
    }   
}

根据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>

根据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>

我只想将SOAP响应中的<ns1:getDoubleResponse>更改为<ns1:TestResult>

如何修复命名空间?我不介意通过DOM或Xpath抛出响应。 我也有兴趣扩展Zend_Soap_Server以自定义响应。

更新

我已经使用类扩展了Zend_Soap_Server,现在尝试通过handle()方法发送自定义响应。

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

这是扩展Zend_Soap_Server并处理响应的类:

// 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;       
    }
}

但现在,当我在SOAP UI中运行请求时,我看到一个空响应。不知道我做错了什么。

2 个答案:

答案 0 :(得分:5)

最后,我决定在我的Controller中手动解析传入的SOAP请求:

// 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;
    }
}

答案 1 :(得分:1)

我在jax ws实现中有类似的要求。令人惊讶的是我发现了,

Here

同样,我不认为zend框架可以控制消息名称。阅读zend规范进行确认。

您只有选项是在使用DOM或Xpath在客户端接收后根据您的要求解析响应。我不熟悉zend框架,所以现在不能给你工作代码。