我正在尝试使用Zend_Soap_Server
PHP 中的类来实现 SOAP 服务器。
这webservice.php
是请求入口点的文件:
<?php
require_once 'library.php';
require_once 'Zend/Loader/Autoloader.php';
$autoloader = \Zend_Loader_Autoloader::getInstance();
class Math
{
/**
* This method takes ...
*
* @param integer $inputParam
* @return \Library\IncrementedInt
*/
public function increment($inputParam)
{
return new \Library\IncrementedInt($inputParam);
}
}
$options = array('uri' => 'http://localhost' . $_SERVER['REQUEST_URI']);
if (isset($_GET['wsdl'])){
$server = new Zend_Soap_AutoDiscover();
$server->setClass('Math');
}
else {
$server = new Zend_Soap_Server(null, $options);
$server->setClass('Math');
$server->setObject(new Math());
}
$server->handle();
我有library.php
这样的文件:
<?php
namespace Library;
class IncrementedInt
{
public $original;
public $incremented;
public function __construct($num)
{
$this->original = $num;
$this->incremented = ++$num;
}
}
调用http://localhost/webservice.php?wsdl
将输出:
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://localhost/webservice.php" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="Math" targetNamespace="http://localhost/webservice.php">
<script/>
<types>
<xsd:schema targetNamespace="http://localhost/webservice.php">
<xsd:complexType name="\Library\IncrementedInt">
<xsd:all/>
</xsd:complexType>
</xsd:schema>
</types>
<portType name="MathPort">
<operation name="increment">
<documentation>This method takes ...</documentation>
<input message="tns:incrementIn"/>
<output message="tns:incrementOut"/>
</operation>
</portType>
<binding name="MathBinding" type="tns:MathPort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="increment">
<soap:operation soapAction="http://localhost/webservice.php#increment"/>
<input>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/webservice.php"/>
</input>
<output>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/webservice.php"/>
</output>
</operation>
</binding>
<service name="MathService">
<port name="MathPort" binding="tns:MathBinding">
<soap:address location="http://localhost/webservice.php"/>
</port>
</service>
<message name="incrementIn">
<part name="inputParam" type="xsd:int"/>
</message>
<message name="incrementOut">
<part name="return" type="tns:\Library\IncrementedInt"/>
</message>
</definitions>
现在来测试我使用的功能soapUI 4.5.1
,它是一个实现 SOAP 客户端的 Java 应用程序。给它 URIhttp://localhost/webservice.php?wsdl
应该会导致increment
提取函数,但不会。相反,它会提示错误:The Value '\Library\IncrementInt' is an invalid name
. 在我看来,它在接受\
作为类型名称的一部分时遇到了问题。另一方面PHP也离不开它们。
为了确保其他一切正常,我在没有命名空间的情况下测试了完全相同的文件,并且运行顺利。
有没有人遇到过类似的问题,更重要的是,有人知道如何克服这个问题吗?
[更新]
我设法用 ZF2 测试了相同的场景并且它有效。也许我不得不放弃ZF1!