Friday, October 21, 2011

Web Services - PHP, SOAP, WSDL

My web site : Info Tech Vedas
 
Web services SOAP, WSDL example
1.  WSDL is an XML grammar used to describe a web service.  It defines how the web service is accessed and operations it performs how messages are passed and the structure of the messages.  In this post, we talk about anatomy of WSDL.
    Below is anatomy of WSDL document:
        <definitions xmlns="http://schemas.xmlsoap.org/wsdl/">
            <types>
            <!-- definition of types used in WSDL-->
            </types>

            <message>
            <!--abstract definition of the data being transmitted-->
            </message>

            <portType>
            <!-- a set of abstract operations referring to input and output messages  -->
            </portType>

            <binding>
            <!-- concrete protocol and data format specs -->
            </binding>

            <service>
            <!-- specifies locations and bindings for a service -->
            </service>
        </definitions>

    The funny thing,  you may look simple structure above. May you imagine it will simple and short document. No! It will longer document than you dream! So, I will talk step by step each part of the document.
    As result of this practice, we will collaborate with SOAP to generate services. This tutorial must be run at PHP 5.
    Next post, we will talk type in more detail. Don't miss it!
   
2. Now, we will look sample WSDL document. In this sample, we want to load text from server. User put their name (such as "I like this example"), then server return text "hello, I like this example ". Our web services named "hello". Below our WSDL for hello:
    <?xml version="1.0"?>
    <definitions name="HelloWorld" targetNamespace="urn:HelloWorld" xmlns:tns="urn:HelloWorld"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
        <types>
            <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Hello">
            <xsd:element name="getName" type="xsd:string" />
            <xsd:element name="HelloResponse" type="xsd:string" />          
            </xsd:schema>           
        </types>
            <message name="doHello">
                <part name="yourName" type="tns:getName" />
            </message>
            <message name="doHelloResponse">
                <part name="return" type="tns:HelloResponse" />
            </message>  
        <portType name="HelloPort">
            <operation name="doHello">
                <input message="tns:doHello" />
                <output message="tns:doHelloResponse" />
            </operation>
        </portType>
        <binding name="HelloBinding" type="tns:HelloPort">
            <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
            <operation name="doHello">
                <soap:operation soapAction="urn:HelloAction" />
                    <input>
                        <soap:body use="encoded" namespace="urn:Hello" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />         
                    </input>
                    <output>
                        <soap:body use="encoded" namespace="urn:Hello" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />         
                    </output>
            </operation>
        </binding>
        <service name="HelloService">
            <port name="HelloPort" binding="tns:HelloBinding">
            <soap:address location="http://localhost/test/wsdl/hello_server.php" />
            </port>
        </service>
    </definitions>

    Create a file named "hello.wsdl" within www/test/wsdl. Enter code above.
    From the code, you can see structure like previous article.
   
3.   The types element encapsulates the data type definitions used when messages are exchanged. XML schema is the preferred language. It offer maximum interoperability.
    <types>
        <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Hello">
        <xsd:element name="getName" type="xsd:string" />
        <xsd:element name="HelloResponse" type="xsd:string" />
        </xsd:schema>
    </types>

    In our practice, we want to pass simple string. So, we write xsd:string. Other, you can use numeric type. For strings or numerics, we can handle with simple way xsd:string or xsd:int which does not get defined here. For objects and arrays, we will talk next time.

4.    A message is an abstract definition of the data being transmitted.  Message defines input and output parameters. Input which you send parameter to server. Output which you get result from server.  Look our code:
    <message name="doHello">
        <part name="yourName" type="tns:getName" />
    </message>

    <message name="doHelloResponse">
        <part name="return" type="tns:HelloResponse" />
    </message>

    Relationship message and types can you read below:

    Yup, every parameters be transmitted have type. These types, you define at types. It is simple, isn't it?

5.   A port type is a named set of abstract operations and the abstract messages involved.  A port type element contains a collection of operations and associates the message used with them.  Look at our practice below:
    <portType name="HelloPort">
        <operation name="doHello">
        <input message="tns:doHello" />
        <output message="tns:doHelloResponse" />
        </operation>
    </portType>

    To look relationship with messages, see below:

    Port type element contains any number of child operation elements. An operation element exists for each operation that will exposed by web services named through its name attribute. The value of the name of attribute is the name of the corresponding PHP function.
    At the client side, the name attribute is the name of the function you will be calling.
    Next, we will talk about Binding.
   
6.    A Binding specifies concrete details about a portType and a protocol. We can use three types of bindings in WSDL 1.1: SOAP, HTTP, and MIME. In this tutorial, we use SOAP bindings only.
    <binding name="HelloBinding" type="tns:HelloPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
        <operation name="doHello">
            <soap:operation soapAction="urn:HelloAction" />
                <input>
                    <soap:body use="encoded" namespace="urn:Hello" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />         
                </input>
                <output>
                    <soap:body use="encoded" namespace="urn:Hello" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />         
                </output>
        </operation>
    </binding>

    To simply, look this picture:

    Next, we will talk about services.

7.    The service element describe a particular web service by providing a name and the location and associates a binding to a particular port. Sample, look at this:
    <service name="HelloService">
        <port name="HelloPort" binding="tns:HelloBinding">
            <soap:address location="http://localhost/test/wsdl/hello_server.php" />
        </port>
    </service>

    We can see, a port must specify only address locations and no more than one address. Under SOAP, you do this using the soap:address element.

8.    We have finished talk about WSDL document. Now, we will collaborate with SOAP. This soap is provided at PHP.
    create a file named "hello_server.php" within folder where hello.wsdl has placed (within www/test/wsdl). Enter following code:
    <?php
        if(!extension_loaded("soap")){
        dl("php_soap.dll");
        }

        ini_set("soap.wsdl_cache_enabled","0");
        $server = new SoapServer("hello.wsdl");

        function doHello($yourName){
        return "Hello, ".$yourName;
        }

        $server->AddFunction("doHello");
        $server->handle();
    ?>

This service has one method "doHello".  And this method need a parameter named $yourName.  It will return text such as "Hello, ".$yourName.

9.    Last, we create client using SOAP.
    Create a file named "hello_client.php", enter following code:
    <?php
        try{
            $sClient = new SoapClient('http://localhost/test/wsdl/hello.wsdl');
            $params = "I like this example";
            $response = $sClient->doHello($params);
            var_dump($response);
        }
        catch(SoapFault $e){
            var_dump($e);
        }
    ?>

No comments:

Post a Comment