Before creating Web Service in PHP first of all I will discuss about what’s Web Service? Web Service is an application that is designed to interact directly with other applications over the internet. That is in simple term, Web Services are means for interacting with objects over the Internet. Web Service is stateless service architecture which is platform independent, language independent and protocol independent.

  The main advantage of Web Service communication is web service messages are formatted as XML, a standard way for communication between two incompatible systems. And this message is sent via HTTP, so that they can reach to any machine on the internet without being blocked by firewall.

Web Services communicate by using standard web protocols and data formats, such as XML, SOAP, WSDL, UDDI etc.

XML: XML is used to tag data in web service because XML provides a language which can be used between different platforms and programming languages, and still express complex messages and functions.

SOAP: SOAP is stands for Simple Object Access Protocol. It’s used to transferring data in web service.

WSDL: WSDL is stands for Web Service Description Language and it’s used to describing information about web service.

UDDI: its stands for Universal Description Discovery and Integration. It’s a protocol for describing available web service components. This standard allows businesses to register with an Internet directory that will help them advertise their services.             

Now let’s take a look on how to create web service in PHP.  To create a web service in PHP you have to follow some steps.

1.       Create WSDL (Web Service Description Language)file for web services

2.       Create Server site file

3.       Create Client site file

Create WSDL (Web Service Description Language): To create web service description language you have to follow a specific format. Here I’m creating WSDL file with name ‘catalog.wsdl’, server file with name ‘soap-server.php’ and client site file with name ‘soap-client.php’. Let’s take a look on it.

‘catalog.wsdl’ files code:
<?xml version ='1.0'
encoding ='UTF-8' ?>
<definitions name='Catalog'
  xmlns:tns=' '
  xmlns:soap =''
  xmlns:xsd =''
  xmlns:soapenc =''
  xmlns:wsdl =''
  xmlns='http : //'>
<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='tns:CatalogPortType'>
  <soap:binding style='rpc'     transport=''  />
  <operation name='getCatalogEntry'>
    <soap:operation soapAction='urn:localhost-catalog#getCatalogEntry'/>
      <soap:body use='encoded' namespace='urn:localhost-catalog'
      <soap:body use='encoded' namespace='urn:localhost-catalog'
<service name='CatalogService'>
  <port name='CatalogPort' binding='CatalogBinding'>
    <soap:address location='http://localhost:85/soap-server.php'/>

‘soap-server.php’ files code:
// call web service method
function getCatalogEntry($catalogId) {
// check conditions    
return "ArunCatalog1";
elseif ($catalogId=='catalog2')
return "ArunCatalog2";
ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer("catalog.wsdl");

‘soap-client.php’ files code:
  //include 'catalog.wsdl';
  $soapClient=  new SoapClient("catalog.wsdl");
  $catalogid ='catalog1';
  // call web service method
  $responseResult = $soapClient->getCatalogEntry($catalogid);
  echo $responseResult;

 Now put these files in Apache Server -> htdocs directory and start Apache Server.

How to create Web Service in PHP

Now click on ‘soap-client.php’ file name.

How to create Web Service in PHP

This is a complete description on creating web service in PHP. I hope it will be useful for you.

  Modified On Sep-07-2019 12:13:15 AM

Leave Comment