Home > DeveloperSection > Articles > How to create Web Service in PHP

How to create Web Service in PHP


PHP PHP 
Ratings:
0 Comment(s)
 4793  View(s)
Rate this:

How to create Web Service in PHP

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'

  targetNamespace='http://example.org/catalog'

  xmlns:tns=' http://example.org/catalog '

  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'

  xmlns:xsd='http://www.w3.org/2001/XMLSchema'

  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'

  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'

  xmlns='http://schemas.xmlsoap.org/wsdl/'>

 

<message name='getCatalogRequest'>

  <part name='catalogId' type='xsd:string'/>

</message>

<message name='getCatalogResponse'>

  <part name='Result' type='xsd:string'/>

</message>

 

<portType name='CatalogPortType'>

  <operation name='getCatalogEntry'>

    <input message='tns:getCatalogRequest'/>

    <output message='tns:getCatalogResponse'/>

  </operation>

</portType>

 

<binding name='CatalogBinding' type='tns:CatalogPortType'>

  <soap:binding style='rpc'

    transport='http://schemas.xmlsoap.org/soap/http'

  />

  <operation name='getCatalogEntry'>

    <soap:operation soapAction='urn:localhost-catalog#getCatalogEntry'/>

    <input>

      <soap:body use='encoded' namespace='urn:localhost-catalog'

        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>

    </input>

    <output>

      <soap:body use='encoded' namespace='urn:localhost-catalog'

        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>

    </output>

  </operation>

</binding>

 

<service name='CatalogService'>

  <port name='CatalogPort' binding='CatalogBinding'>

    <soap:address location='http://localhost:85/soap-server.php'/>

  </port>

</service>

</definitions>

‘soap-server.php’ files code:

<?php

// call web service method

function getCatalogEntry($catalogId) {

// check conditions    

if($catalogId=='catalog1')

return "ArunCatalog1";

elseif ($catalogId=='catalog2')

return "ArunCatalog2";

}

 

ini_set("soap.wsdl_cache_enabled", "0");

$server = new SoapServer("catalog.wsdl");

$server->addFunction("getCatalogEntry");

$server->handle();

 

?>

‘soap-client.php’ files code:

<?php

 

  //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.


Don't want to miss updates? Please click the below button!

Follow MindStick