PHP Soap Server, Part 2: PHP Server and Client

This is in conclusion to Part 1

The example soap service that i will build for demonstration purposes will be a simple schedule service, that can be queried to find out what meeting is next and todays meeting schedule.

Sorry for taking so long to put this out, but better late than never, eh?

All the code/wsdl/xml is available here
And the demo client (Source Code) and server (Source Code)

SOAP Server

Constructing a SOAP server in PHP is relatively straight-forward, all that is needed are the following:

  • A Server class
  • A Class for each XML Type used
  • And a few more lines of code to start the SOAPServer class

Server Class

The server class is used by PHP’s Soap server class, and must contain a function for each operation described in the WSDL file. i.e.

class MySoapServer {
    function GetQuote($name) {
        if ($name == "MSFT") return 2;

Dealing with SOAP is pretty transparent, You can return classes that represent the xml types that actually get send to the client, and vice-versa for the data received, as for above, simple types would be used.

XML Classes

In order to accept and return complex types php classes need to be mapped to their xml counterparts,
for example the xml type:

<xsd:complexType name='Meeting'>
    <xsd:element name='Location' type='xsd:string' />
    <xsd:element name='Name' type='xsd:string' />
    <xsd:element name='Importance' type='xsd:string' />
    <xsd:element name='StartTime' type='xsd:string' />
    <xsd:element name='Duration' type='xsd:int' />

Can be converted into this php class:

class Meeting
    var $Location;
    var $Name;
    var $Importance;
    var $StartTime;
    var $Duration;

This can be used in the server class, to accept and return data.
As for xml types such as this:

<xsd:complexType name='Meetings'>
    <xsd:element name='Meeting' maxOccurs='unbounded' minOccurs='1'/>

In php this is treated simply an array of the above Meeting classes

The Server Code

This is all the php code needed to handle a soap request:

$classmap = array('Meeting'=>'Meeting');
$server = new SoapServer('soap.wsdl',array('classmap'=>$classmap));

Here we define which php classes map to which xml types (namely Meeting -> Meeting), we then create the server object, tell it to use our server class to process requests, and finally handle the request.

Thanks for reading, i hope you found this useful!

Bookmark the permalink.

One Response to PHP Soap Server, Part 2: PHP Server and Client

  1. Hi,
    if you want to easily communicate with a SOAP Web service, try projects like wsdl2php or WsdlToPhp. The last one has its own website,
    good luck

Leave a Reply

Your email address will not be published. Required fields are marked *