我正在使用 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"><?xml version="1.0" encoding="utf-8"?>
<IntegerResult><Value>6</Value></IntegerResult></return>
</ns1:getDoubleResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我只想将<ns1:getDoubleResponse>
SOAP 响应更改为类似<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 中运行请求时,我看到一个空响应。不知道我做错了什么。