zend soap服务器响应集自定义ns1名称空间 [英] zend soap server response set custom ns1 namespace

查看:99
本文介绍了zend soap服务器响应集自定义ns1名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

这是我的模型代码:

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,这是我看到的请求:

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>

这是根据SOAP UI的关联响应:

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>

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

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

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

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.

更新:

我用一个类扩展了Zend_Soap_Server,现在尝试通过handle()方法发送自定义响应.

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');

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

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

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

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

推荐答案

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

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

这篇关于zend soap服务器响应集自定义ns1名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆