- Info:
- This
example shows how to use Web Service to return Object or Array or Hash from
Server to Client.
- In order
for this to work we had to omit type of response message: <part name="infoReturn"></part>
- This
approach is not appropriate since WSDL doesn't define class structure so Client
can't get that info from WSDL.
This is
fixed in tutorial PHP � SOAP Extension � Examples � Return Object � Define
structure in WSDL.
- Procedure:
- Create
cllient11.php, server11.php and server11.wsdl in directory accessable from web.
client11.php
<?php
//DISABLE WSDL CACHE.
ini_set("soap.wsdl_cache_enabled", "0");
//CREATE SOAP CLIENT FROM WSDL.
$client = new SoapClient("http://app.lab.aduro.hr/~givor/SelfcareWS/src/server11.wsdl");
//CALL SOAP METHOD.
$test = $client->test();
//DISPLAY RESPONSE.
var_dump($test);
echo($test->name." ".$test->surName); //Return object
data.
//echo($test[0]." ".$test[1]); //Return
array data.
//echo($test["name"]."
".$test["surName"]); //Return hash data.
?>
server11.php
<?php
//CLASS WTH WEB SERVICE METHODS.
class WebService {
function test() {
//OBJECT.
$obj = new stdClass();
$obj -> name = "John";
$obj -> surName = "Malkovic";
//ARRAY.
$arr = array("John", "Malkovic");
//HASH.
$hsh = array("name"=>"John", "surName"=>"Malkovic");
//RETURN.
return $obj; //Return object.
//return $arr; //Retun array.
//return $hsh; //Retun hash
}
//DISABLE WSDL CACHE.
ini_set("soap.wsdl_cache_enabled", "0");
//CREATE SOAP SERVER.
ini_set("soap.wsdl_cache_enabled", "0"); // disabling
WSDL cache
$server = new SoapServer("http://app.lab.aduro.hr/~givor/SelfcareWS/src/server11.wsdl");
$server->setClass("WebService");
$server->handle();
?>
server11.wsdl
<?xml
version='1.0' encoding='UTF-8'?>
<!-- WSDL file generated by PHP WSDLCreator
(http://www.protung.ro) -->
<definitions
name="WSDLExample1" targetNamespace="urn:WSDLExample1"
xmlns:typens="urn:WSDLExample1" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<message
name="infoResponse">
<part name="infoReturn"></part>
</message>
<portType
name="SelfcareWS1PortType">
<operation
name="test">
<documentation>Adds two numbers.</documentation>
<output
message="typens:infoResponse"></output>
</operation>
</portType>
<binding
name="SelfcareWS1Binding" type="typens:SelfcareWS1PortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
<operation
name="test">
<soap:operation soapAction="urn:SelfcareWS1Action"></soap:operation>
<input>
<soap:body
namespace="urn:WSDLExample1" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body>
</input>
<output>
<soap:body
namespace="urn:WSDLExample1" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body>
</output>
</operation>
</binding>
<service
name="WSDLExample1Service">
<port
name="SelfcareWS1Port" binding="typens:SelfcareWS1Binding">
<soap:address location="http://app.lab.aduro.hr/~givor/SelfcareWS/src/server10.php"></soap:address>
</port>
</service>
</definitions>
output
(For Object)
object(stdClass)#2 (2) {
嚤"name"]=>
澵tring(4) "John"
嚤"surName"]=>
澵tring(8)
"Malkovic"
}
John Malkovic
output
(For Array)
array(2) {
嚤0]=>
澵tring(4) "John"
嚤1]=>
澵tring(8)
"Malkovic"
}
output
(For Hash)
array(2) {
嚤"name"]=>
澵tring(4) "John"
嚤"surName"]=>
澵tring(8)
"Malkovic"
}
沒有留言:
張貼留言