WSDL 和 SOAP:返回带有方法的对象

发布于 2024-12-06 19:49:37 字数 382 浏览 1 评论 0原文

有没有办法用肥皂的方法返回一个对象?如果我在 WSDL 中返回 xsd:struct,我只能获取对象的属性,但不能使用任何方法。

例如

class person
{
  var $name = "My name";
  public function getName()
  {
      return $this->name;
  }
}

,在获取对象后:

$client = new SoapClient();
$person = $client->getPerson();
echo $person->getName(); // Return "My Name";

谢谢。

Is there a way to return in soap an object with his methods? If i return xsd:struct in WSDL i only get the properties of the object but i can't use any of the methods.

For example

class person
{
  var $name = "My name";
  public function getName()
  {
      return $this->name;
  }
}

So after fetching the object:

$client = new SoapClient();
$person = $client->getPerson();
echo $person->getName(); // Return "My Name";

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-12-13 19:49:37

您无法使用 SOAP 来做到这一点。基本上,您的 PHP 类被映射到由 XML 模式定义的 XML 数据结构。此映射仅包含属性,不能包含可执行代码。 SOAP 是为互操作性而设计的,当然您不能在 PHP 和 Java 或 .NET 之间共享代码。在接收端,您的 XML 数据结构将转换为客户端编程语言的数据结构(如果您使用 SoapClient 则为 PHP 类,如果您使用 C# 类,则为 C# 类)代码>C#)。由于 XML 数据结构仅携带属性信息,因此无法重建原始类的可执行部分。

但是如果 SOAP 服务器和连接客户端都可以访问相同的代码库(这意味着相同的类),那么有一件事会有所帮助。您可以使用 classmapSoapClient 的构造函数中定义 XML 类型和 PHP 类之间的映射-选项。这允许 SoapClient 将传入的 XML 数据结构映射到真正的 PHP 类 - 考虑到服务器和客户端都可以访问相关的类定义。这允许您使用 SOAP 通信接收端的方法。

class book {
    public $a = "a";
    public $b = "c";
    public function getName() {
        return $this->a.' '.$this->b;
    }
}

$options = array(
    'classmap' => array('book' => 'book')
);
$client = new SoapClient('path/to/wsdl', $options);
$book    = $client->test();
echo $book->getName();

WSDL 可能看起来像(从 SoapClient 测试之一复制并进行修改):

<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.nothing.com" targetNamespace="http://schemas.nothing.com">
    <wsdl:types>
        <xsd:schema targetNamespace="http://schemas.nothing.com">
            <xsd:complexType name="book">
                <xsd:all>
                    <xsd:element name="a" type="xsd:string"/>
                    <xsd:element name="b" type="xsd:string"/>
                </xsd:all>
            </xsd:complexType>
  </xsd:schema>
    </wsdl:types>
    <message name="testRequest">
    </message>
    <message name="testResponse">
        <part name="res" type="tns:book"/>
  </message>
    <portType name="testPortType">
        <operation name="test">
            <input message="tns:testRequest"/>
            <output message="tns:testResponse"/>
        </operation>
    </portType>
    <binding name="testBinding" type="tns:testPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="test">
            <soap:operation soapAction="http://localhost:81/test/interface.php?class=test/dotest" style="rpc"/>
            <input>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/>
            </input>
            <output>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/>
            </output>
        </operation>
    </binding>
    <service name="test">
        <port name="testPort" binding="tns:testBinding">
            <soap:address location="http://localhost:81/test/interface.php?class=test"/>
        </port>
    </service>
</wsdl:definitions>

如果您在 PHP 中使用 SOAP,那么 PHP 中的 SOAP 状态 可能会很有趣。

You cannot do this with SOAP. Basically your PHP class is being mapped to an XML data structure that's defined by an XML schema. This mapping only includes properties and cannot include executable code. SOAP is designed for interoperability and naturally you cannot share code between, let's say, PHP and Java or .NET. On the receiving side your XML data structure is being transformed into a data structure of the client's programming language (a PHP class if you use SoapClient or a C# class if you use C#). As the XML data structure only carries property information the executable part of the originating class cannot be rebuilt.

But there is one thing that can help if both the SOAP server and the connecting client have access to the same code base (which means the same classes). You can define a mapping between a XML type and a PHP class in the SoapClient's constructor using the classmap-option. This allows SoapClient to map incoming XML data structures to real PHP classes - given the fact that both the server and the client have access to the relevant class definition. This allows you to use methods on the receiving side of the SOAP communication.

class book {
    public $a = "a";
    public $b = "c";
    public function getName() {
        return $this->a.' '.$this->b;
    }
}

$options = array(
    'classmap' => array('book' => 'book')
);
$client = new SoapClient('path/to/wsdl', $options);
$book    = $client->test();
echo $book->getName();

The WSDL might look like (copied from one of the SoapClient tests and adpated):

<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.nothing.com" targetNamespace="http://schemas.nothing.com">
    <wsdl:types>
        <xsd:schema targetNamespace="http://schemas.nothing.com">
            <xsd:complexType name="book">
                <xsd:all>
                    <xsd:element name="a" type="xsd:string"/>
                    <xsd:element name="b" type="xsd:string"/>
                </xsd:all>
            </xsd:complexType>
  </xsd:schema>
    </wsdl:types>
    <message name="testRequest">
    </message>
    <message name="testResponse">
        <part name="res" type="tns:book"/>
  </message>
    <portType name="testPortType">
        <operation name="test">
            <input message="tns:testRequest"/>
            <output message="tns:testResponse"/>
        </operation>
    </portType>
    <binding name="testBinding" type="tns:testPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="test">
            <soap:operation soapAction="http://localhost:81/test/interface.php?class=test/dotest" style="rpc"/>
            <input>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/>
            </input>
            <output>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/>
            </output>
        </operation>
    </binding>
    <service name="test">
        <port name="testPort" binding="tns:testBinding">
            <soap:address location="http://localhost:81/test/interface.php?class=test"/>
        </port>
    </service>
</wsdl:definitions>

The State of SOAP in PHP might be interesting if you're doing SOAP in PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文