PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Developing Web Services Using PHP

by Deepak Vohra

A web service consists of a server to serve requests to the web service and a client to invoke methods on the web service. The PHP class library provides the SOAP extension to develop SOAP servers and clients and the XML-RPC extension to create XML-RPC servers and clients. Before I delve further into developing web services with PHP, I shall briefly discuss web services.

Introduction to Web Services

A web service is a software system designed for interoperable interaction over a network. A web service is defined with a WSDL (Web Services Description Language) document, and other systems interact with the web service using SOAP messages, transferred using HTTP with an XML serialization. A web service is an abstract resource that provides a set of functions and is implemented by an agent, which sends and receives messages. A provider entity provides the functionality of a web service with a provider agent and a requester entity uses the web service functionality with a requester agent. Web services implement various technologies, some of which are XML, SOAP, and WSDL. XML is a standard format for data exchange. Web service requests and responses are sent as XML messages. The elements and attributes that may be specified in an XML document are specified in an XML Schema. SOAP provides a standard framework for packaging and exchanging XML messages. WSDL is an XML document in the namespace for describing a web service as a set of endpoints operating on messages. A WSDL document specifies the operations (methods) provided by a web service and the format of the XML messages.

Installing the PHP Web Services Extensions

The SOAP and XML-RPC extensions are packaged with the PHP 5 installation. The SOAP extension and the XML-RPC extension are not enabled by default in a PHP installation. To enable the SOAP and XML-RPC extensions add the following extension directives in the php.ini configuration file.


Restart the Apache 2 server to activate the SOAP and XML-RPC extensions. The SOAP extension supports subsets of the SOAP 1.1, SOAP 1.2, and WSDL 1.1 specifications.

Creating a SOAP Web Service

After activating the SOAP extension in the PHP configuration file, a SOAP server and a SOAP client may be created using the SOAP PHP class library. A SOAP server serves web service requests and a SOAP client invokes methods on the SOAP web service. The SOAP library provides various functions for creating a SOAP server and a SOAP client. Some of the commonly used SOAP functions are discussed in Table 1.

Table 1. SOAP functions

Method Description
SoapServer->__construct( mixed wsdl [, array options] ) Creates a SoapServer object. The wsdl parameter specifies the URI of the WSDL. SoapServer options such as SOAP version may be specified in the options array.
SoapServer->addFunction( mixed functions ) Adds one or more PHP functions that will handle SOAP requests. A single function may be added as a string. More than one function may be added as an array.
SoapServer->fault() SoapServer fault indicating an error.
SoapServer->getFunctions() Returns a list of functions.
SoapServer->handle() Processes a SOAP request, invokes required functions and sends back a response.
SoapServer->setClass(string class_name [, mixed args [, mixed ...]] ) Sets the class that will handle SOAP requests. Exports all methods from the specified class. The args are used by the default class constructor.
SoapHeader->__construct() Creates a SOAP header.
SoapClient->__soapCall( string function_name, array arguments [, array options [, mixed input_headers [, array &output_headers]]] ) Invokes a SOAP function.
SoapClient->__doRequest() Performs a SOAP request.
SoapClient->__getFunctions() Returns a list of SOAP functions.
SoapClient->__getTypes() Returns a list of SOAP types.

Creating a SOAP Server

Before we create a SOAP server we need to create a WSDL document defining the web service. The WSDL document defines the operations that the web service provides. I will create an example web service that provides an operation getCatalogEntry, which returns a catalog entry for a catalog ID. A WSDL is an XML document in the namespace. Some of the elements of a WSDL document are discussed in Table 2.

Table 2. WSDL elements

Element Description
definitions Root element of a WSDL document.
types Specifies data type definitions for the messages exchanged by the web service. XML Schema is the recommended type system.
message Defines the data being transmitted. A message consists of one or more parts. A part is associated with a type.
portType Defines a set of operations and the input-output messages for each operation.
operation An action (method) supported by the service. Each operation consists of input and output messages.
input Specifies a message format for the request.
output Specifies a message format for the response.
binding Defines message format and protocol details for operations and messages for a particular portType.
service Specifies a group of ports.
port Defines an endpoint by associating an address with a binding.

Next, create a WSDL document for the example web service. The example WSDL document, catalog.wsdl, defines message elements getCatalogRequest and getCatalogResponse for the request and response messages. In the WSDL document define a portType, CatalogPortType, for the getCatalogEntry operation that returns a catalog entry as a HTML string for a string catalogId. Define a binding, CatalogBinding, for the getCatalogEntry operation and the input output messages. The soap:binding element specifies that the binding is bound to the SOAP protocol format. The soap:operation element specifies information for the operation. The soap:body element specifies how the message parts appear inside the SOAP Body element. Define a service CatalogService that consists of a port, CatalogPort, which is associated with the CatalogBinding binding. The soap:address element specifies the URI of an address. The catalog.wsdl WSDL document is listed below.

<?xml version ='1.0' encoding ='UTF-8' ?> 
<definitions name='Catalog' 
  xmlns:tns=' ' 

<message name='getCatalogRequest'> 
  <part name='catalogId' type='xsd:string'/> 
<message name='getCatalogResponse'> 
  <part name='Result' type='xsd:string'/> 

<portType name='CatalogPortType'> 
  <operation name='getCatalogEntry'> 
    <input message='tns:getCatalogRequest'/> 
    <output message='tns:getCatalogResponse'/> 

<binding name='CatalogBinding' type=
  <soap:binding style='rpc' 
  <operation name='getCatalogEntry'> 
    <soap:operation soapAction='urn:localhost-catalog#
      <soap:body use='encoded' namespace=
      <soap:body use='encoded' namespace=

<service name='CatalogService'> 
  <port name='CatalogPort' binding=
    <soap:address location='http://localhost/

Copy the catalog.wsdl document to the C:\Apache2\htdocs directory, the directory in which PHP scripts are run. Create a PHP script, soap-server.php, to define the operations provided by the CatalogService web service. In the soap-server.php script define a function getCatalogEntry() that takes a catalogId as an argument and returns a string consisting of an HTML document. The HTML document string returned comprises of the catalog entry for the specified catalogId.

Pages: 1, 2, 3, 4

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: