PHP Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.21

PHP SOAP Extension - Server Applications

Part:   1  2  3  4 

PHP Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Non ASCII Characters with MySQL

Inputting Non ASCII Characters

Controlling Response Header Lines

HTTP Request Variables

Sessions

Using Cookies

PHP SOAP Extension

PHP SOAP Extension - Server

Directories, Files and Images

Using MySQL with PHP

... Table of Contents

This chapter describes:

  • PHP SOAP Extension Functions for Server Applications
  • HelloServer.php - First SOAP Server Application
  • HelloServer12.php - SOAP 1.2 Server Application
  • HelloServerWsdl.php - SOAP 1.2 Server Application in WSDL Mode

PHP SOAP Extension Functions for Server Applications

If you read the SOAP Extension reference page, you will see that SOAP Extension support SOAP server applications with a class called SoapServer, which offers the following functions:

  • SoapServer->__construct() - construct a new SoapServer object
  • SoapServer->addFunction() - Adds one or several functions those will handle SOAP requests
  • SoapServer->setClass() - Sets class which will handle SOAP requests
  • SoapServer->handle() - Handles a SOAP request
  • SoapServer->getFunctions() - Returns list of defined functions
  • ...

SoapServer->__construct() allows you to construct a new SoapServer object with the following syntax:

   __construct ( mixed wsdl [, array options] );

where "wsdl" specifies the URL of the WSDL document, and "options" specifies a list of options:

   'uri'          => "...", # the name space of the SOAP service
   'soap_version' => SOAP_1_1 |SOAP_1_2,
   'actor'        => "...", # the actor
   'encoding'     => "...", # the encoding name
   'classmap'     => "...", # a map of WSDL types to PHP classes
   ... 

Note that SoapServer object can be constructed in two modes, WSDL mode and non-WSDL mode:

   __construct( "..."[, array options] ); # WSDL mode, many options
      # are provided by the WSDL document
   __construct( null, array options ); # non-WSDL mode, 
      # 'uri' is a required option.

SoapServer->addFunction() allows you to add one or more functions to handle SOAP requests. The functions to be added are functions defined in the current PHP program file.

   $obj->addFunction("func"); # adds one function
   $obj->addFunction(array("func1",...)); # adds many functions
   $obj->addFunction(SOAP_FUNCTIONS_ALL); # adds all functions

SoapServer->setClass() allows you to all methods in the specified class to handle SOAP requests.

   $obj->setClass("cls");

SoapServer->handle() allows you to return the execution back the SOAP Extension to handle the incoming SOAP request.

   $obj->handle();

SoapServer->getFunctions() allows you to get a list of all functions that have been assigned to handle SOAP requests.

   array $a = $obj->getFunctions();

HelloServer.php - First SOAP Server Application

Okay. Now let's build our first SOAP server. I want the first server to perform a very simple function of return a greeting string based on the input name. Here is my version called, HelloServer.php:

<?php # HelloServer.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
#
function hello($someone) { 
   return "Hello " . $someone . "!";
} 
   $server = new SoapServer(null, 
      array('uri' => "urn://www.herong.home/res"));
   $server->addFunction("hello"); 
   $server->handle(); 
?>

The sever application is ready. Note that:

  • This SOAP server application can not be used as a standalone SOAP server. It needs a HTTP server to receive SOAP requests, and a PHP runtime to act as a CGI to feed SOAP requests.
  • The "uri" value is just an unique identification, used as the namespace for the response message.

The HTTP server I will be using is the MS IIS (Internet Information Services). It has already configured correctly to interface with PHP CGI. For details, see my other book: "Herong's Tutorial Notes on PHP". All I have to do is to move my server application to IIS document directory:

>copy HelloServer.php \Inetpub\wwwroot

(Continued on next part...)

Part:   1  2  3  4 

Dr. Herong Yang, updated in 2006
PHP Tutorials - Herong's Tutorial Notes - PHP SOAP Extension - Server Applications