Implementation of the SOAP server - server.php
:
<?php
// turn off WSDL caching
ini_set("soap.wsdl_cache_enabled","0");
// model, which uses in web service functions as parameter
class Book
{
public $name;
public $year;
}
/**
* Determines published year of the book by name.
* @param Book $book book instance with name set.
* @return int published year of the book or 0 if not found.
*/
function bookYear($book)
{
// list of the books
$_books=[
['name'=>'test 1','year'=>2011],
['name'=>'test 2','year'=>2012],
['name'=>'test 3','year'=>2013],
];
// search book by name
foreach($_books as $bk)
if($bk['name']==$book->name)
return $bk['year']; // book found
return 0; // book not found
}
// initialize SOAP Server
$server=new SoapServer("test.wsdl",[
'classmap'=>[
'book'=>'Book', // 'book' complex type in WSDL file mapped to the Book PHP class
]
]);
// register available functions
$server->addFunction('bookYear');
// start handling requests
$server->handle();
Implementation of the SOAP client - client.php
:
<?php
// model
class Book
{
public $name;
public $year;
}
// create instance and set a book name
$book =new Book();
$book->name='test 2';
// initialize SOAP client and call web service function
$client=new SoapClient('http://a.uz/soap/server.php?wsdl',['trace'=>1,'cache_wsdl'=>WSDL_CACHE_NONE]);
$resp =$client->bookYear($book);
// dump response
var_dump($resp);
Dump of the response:
string '2012' (length=4)
WSDL file test.wsdl
:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="Library"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="Library"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="Library"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<xsd:documentation></xsd:documentation>
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="Library">
<xsd:complexType name="book">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"></xsd:element>
<xsd:element name="year" type="tns:integer"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="bookYearRequest">
<wsdl:part name="book" type="xsd:book"></wsdl:part>
</wsdl:message>
<wsdl:message name="bookYearResponse">
<wsdl:part name="year" type="tns:integer"></wsdl:part>
</wsdl:message>
<wsdl:portType name="Library">
<wsdl:operation name="bookYear">
<wsdl:input message="tns:bookYearRequest"/>
<wsdl:output message="tns:bookYearResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="Library" type="tns:Library">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="bookYear">
<soap:operation soapAction="http://a.uz/soap/server.php"/>
<wsdl:input>
<soap:body use="literal" namespace="Library"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal" namespace="Library"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Library">
<wsdl:port binding="tns:Library" name="BookLibrary">
<soap:address location="http://a.uz/soap/server.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>