6

我正在尝试使用Zend_Soap_ServerPHP 中的类来实现 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!

4

2 回答 2

4

XSD 对象类型\Library\IncrementedInt 不是您概述的 PHP 命名空间中的 PHP 类:

<?php
namespace Library;

class IncrementedInt
{
    ...

前缀类型实际上是:

tns:\Library\IncrementedInt

请注意tns:前面的前缀。你无法在 PHP 中表达它,当你将它扩展到它的命名空间 URI 时,它变得更加清晰:

{http://localhost/webservice.php}\Library\IncrementedInt

此外,Zend SOAP 来自PHP 命名空间(PHP 5.2)之前的时间,因此它需要采取不同的策略来将此类型别名为 PHP 类型(类名)。它采用类型的本地名称:

\Library\IncrementedInt

并用下划线替换该类型名称中的每个“无效”字符:

_Library_IncrementedInt

因此,您需要以这种方式命名该类:

<?php

class _Library_IncrementedInt
{
    ...

如果你想让它开箱即用。


然后,您通过评论要求澄清:

所以你的意思是,我无法将命名空间类作为类型名称,对吧?

好吧,至于默认行为很可能不会,但是由于涉及到术语策略,我想说有一些机制可以为该操作扩展 Zend Soap 并注入一个不同的机制,例如确实允许命名空间类(类通过他们的 FQCN),请参阅策略模式

如果这只是一个单一的类,你可以同时做一些便宜的把戏,并将你的命名空间类扩展为短名称或使用class_alias.

于 2013-09-17T09:03:08.690 回答
1

Zend_Soap_AutoDiscover从 phpdoc 块中获取类型名称,即,来自此注释行:

* @return \Library\IncrementedInt

例如,您可以将该类型更改为IncrementedInt,而无需命名空间和斜杠。

于 2013-09-15T18:52:41.160 回答