【问题标题】:zend soap server response set custom ns1 namespacezend soap 服务器响应设置自定义 ns1 命名空间
【发布时间】:2013-12-30 09:40:23
【问题描述】:

我正在使用 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 响应中的 &lt;ns1:getDoubleResponse&gt; 更改为 &lt;ns1:TestResult&gt; 之类的东西

如何修复命名空间?我不介意通过 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 中运行请求时,我看到一个空响应。不知道我做错了什么。

【问题讨论】:

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


    【解决方案1】:

    最后,我决定在我的控制器中手动解析传入的 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;
        }
    }
    

    【讨论】:

    • 我正在尝试这个解决方案,但我的一些回复被切断了,例如&lt;/SOAP-ENV:Envelo 而不是&lt;/SOAP-ENV:Envelope&gt;。 SOAP 响应是否有固定长度?
    【解决方案2】:

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

    Here

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

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

    【讨论】:

    • 好吧,客户端不喜欢解析响应。所以我不得不按照他的要求发送回复。
    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 2013-07-16
    • 2015-07-28
    • 2012-05-28
    相关资源
    最近更新 更多