PHP5的命名空间在实现Web服务时导致不正确的WSDL类型名称

Meh*_*ran 6 php wsdl web-services zend-framework

我正在尝试使用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();
Run Code Online (Sandbox Code Playgroud)

我有这样的library.php文件:

<?php
namespace Library;

class IncrementedInt
{
    public $original;
    public $incremented;

    public function __construct($num)
    {
        $this->original = $num;
        $this->incremented = ++$num;
    }
}
Run Code Online (Sandbox Code Playgroud)

调用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>
Run Code Online (Sandbox Code Playgroud)

现在测试我使用的功能soapUI 4.5.1,它是一个实现SOAP客户端的Java应用程序.给它提供URI http://localhost/webservice.php?wsdl应该导致函数increment提取但不会.相反,它会提示错误:The Value '\Library\IncrementInt' is an invalid name.在我看来,它在接受\作为类型名称的一部分时遇到了问题.另一方面,PHP离不开它们.

为了确保其他一切都正常,我测试了没有命名空间的完全相同的文件,它运行顺利.

有没有人遇到类似的问题,更重要的是,有谁知道如何克服这个问题?

[UPDATE]

我设法用ZF2测试相同的场景,它的工作原理.也许我不得不放弃ZF1!

hak*_*kre 4

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

<?php
namespace Library;

class IncrementedInt
{
    ...
Run Code Online (Sandbox Code Playgroud)

前缀类型实际上是:

tns:\Library\IncrementedInt
Run Code Online (Sandbox Code Playgroud)

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

{http://localhost/webservice.php}\Library\IncrementedInt
Run Code Online (Sandbox Code Playgroud)

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

\Library\IncrementedInt
Run Code Online (Sandbox Code Playgroud)

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

_Library_IncrementedInt
Run Code Online (Sandbox Code Playgroud)

因此,您需要这样命名该类:

<?php

class _Library_IncrementedInt
{
    ...
Run Code Online (Sandbox Code Playgroud)

如果您想让它开箱即用。


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

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

好吧,就默认行为而言很可能不会,但是当涉及术语“策略”时,我想说有某种机制可以为该操作扩展 Zend Soap 并注入一个不同的机制,例如允许命名空间类(类通过他们的 FQCN),请参阅策略模式

如果这只是一个类,您可以同时做一些廉价的技巧,并将您的命名空间类扩展为短名称或别名class_alias