WSDL and SOAP: Return object with methods
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 f开发者_如何学Cetching the object:
$client = new SoapClient();
$person = $client->getPerson();
echo $person->getName(); // Return "My Name";
Thanks.
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.
精彩评论