Zend framework2 ZF2和实现返回复杂类型的SOAP服务器
我试图在PHP5.5中使用Zend Framework 2实现一个SOAP服务器。我已经做到了这一点: library.phpZend framework2 ZF2和实现返回复杂类型的SOAP服务器,zend-framework2,complextype,soapserver,Zend Framework2,Complextype,Soapserver,我试图在PHP5.5中使用Zend Framework 2实现一个SOAP服务器。我已经做到了这一点: library.php <?php namespace Library; class IncrementedInt { /** * @var integer **/ public $original; /** * @var integer **/ public $incremented; public
<?php
namespace Library;
class IncrementedInt
{
/**
* @var integer
**/
public $original;
/**
* @var integer
**/
public $incremented;
public function __construct($num)
{
$this->original = $num;
$this->incremented = ++$num;
}
}
<?php
require_once 'library.php';
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
class Math
{
/**
* This method takes ...
*
* @param integer $inputParam
* @return \Library\IncrementedInt
*/
public function increment($inputParam)
{
return new \Library\IncrementedInt($inputParam);
}
}
if (isset($_GET['wsdl'])) {
$autodiscover = new \Zend\Soap\AutoDiscover();
$autodiscover->setClass('Math')
->setBindingStyle(array('style' => 'document'))
->setUri('http://localhost' . $_SERVER['SCRIPT_NAME']);
header('Content-type: application/xml');
echo $autodiscover->toXml();
}
else {
// pointing to the current file here
$soap = new \Zend\Soap\Server('http://localhost' . $_SERVER['SCRIPT_NAME'] . '?wsdl');
$soap->setClass('Math');
$soap->handle();
}
如您所见,已定义了IncrementedInt
类及其属性original
和incremented
。但是,当我通过发送此XML调用服务时(使用soapUI
):
2.
服务器将做出如下响应:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/webservice.php" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:incrementResponse>
<return xsi:type="ns1:IncrementedInt"/>
</ns1:incrementResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
正如您所看到的,返回值没有展开,就像类型被命名一样。是否有人在ZF2 SOAP服务器中成功返回了复杂类型?怎么用?我在互联网上搜索了一个例子,但没有找到 事实证明,我所缺少的只是Zend Framework默认缓存WSDL的事实。所以我需要做的就是:
<?php
require_once 'library.php';
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
class Math
{
/**
* This method takes ...
*
* @param integer $inputParam
* @return \Library\IncrementedInt
*/
public function increment($inputParam)
{
return new \Library\IncrementedInt($inputParam);
}
}
if (isset($_GET['wsdl'])) {
$autodiscover = new \Zend\Soap\AutoDiscover();
$autodiscover->setClass('Math')
->setUri('http://localhost' . $_SERVER['SCRIPT_NAME']);
header('Content-type: application/xml');
echo $autodiscover->toXml();
}
else {
// pointing to the current file here
$soap = new \Zend\Soap\Server('http://localhost' . $_SERVER['SCRIPT_NAME'] . '?wsdl', array('cache_wsdl' => WSDL_CACHE_NONE));
$soap->setClass('Math');
$soap->handle();
}
toXml();
}
否则{
//在这里指向当前文件
$soap=new\Zend\soap\Server('http://localhost“.$”服务器['SCRIPT\u NAME']。?wsdl',数组('cache\u wsdl'=>wsdl\u cache\u NONE));
$soap->setClass('Math');
$soap->handle();
}
这些变化是:
服务器时,它被设置为禁用WSDL缓存的选项
->setBindingStyle(数组('style'=>'document'))
对自动发现的方法调用被省略,因为它会导致麻烦并阻止成功的SOAP调用
我有点困惑,你是如何在PHP5.2上运行ZendFramework2的?如果你打算使用traits,最低要求是5.3.3或5.4。好的,我知道了,它是PHP5.5。我只是说PHP已经有了名称空间,仅此而已。我会更新这个问题,谢谢你纠正我。你为什么不使用内置zf2 soap服务器/客户端?也许是误会了,因为我用的就是这个!
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/webservice.php" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:incrementResponse>
<return xsi:type="ns1:IncrementedInt"/>
</ns1:incrementResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<?php
require_once 'library.php';
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
class Math
{
/**
* This method takes ...
*
* @param integer $inputParam
* @return \Library\IncrementedInt
*/
public function increment($inputParam)
{
return new \Library\IncrementedInt($inputParam);
}
}
if (isset($_GET['wsdl'])) {
$autodiscover = new \Zend\Soap\AutoDiscover();
$autodiscover->setClass('Math')
->setUri('http://localhost' . $_SERVER['SCRIPT_NAME']);
header('Content-type: application/xml');
echo $autodiscover->toXml();
}
else {
// pointing to the current file here
$soap = new \Zend\Soap\Server('http://localhost' . $_SERVER['SCRIPT_NAME'] . '?wsdl', array('cache_wsdl' => WSDL_CACHE_NONE));
$soap->setClass('Math');
$soap->handle();
}