• home
  • forum
  • my
  • kt
  • download
  • PHP SOAP Extension

    Author: 2008-08-15 07:30:47 From:

    This chapter describes:

    • What is PHP SOAP Extension?
    • My first example with SOAP Extension.
    • SOAP Extension functions for client applications.
    • How to dump detailed information for debugging.
    • A WSDL document example.
    • How use SOAP Extension in non-WSDL mode.

    What Is PHP SOAP Extension?

    PHP SOAP Extension is one of the most popular PHP implementations of SOAP 1.1 and 1.2, developed by The PHP Group.

    What are the features of PHP SOAP Extension? I don't known. I tried to find the feature list on the Web, no success.

    How to install PHP SOAP Extension? It is included in the PHP 5.0.4 package for Windows distributed by Zend Technologies. See previous notes on details of how to install the PHP package and the PHP SOAP Extension.

    GetTemp.php - First Example with SOAP

    To show you an example of how PHP SOAP Extension can be used in a SOAP client application, here is my first SOAP PHP program, GetTemp.php

    <?php # GetTemp.php
    # Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
    #
       $client = new SoapClient
          ("http://www.xmethods.net/sd/2001/DemoTemperatureService.wsdl");
       echo("\nReturning value of getTemp() call: ".
          $client->getTemp("12345"));
    ?>
    

    Run it while your connected to the Internet. You will get:

    >php GetTemp.php
    
    Returning value of getTemp() call: 52
    

    Very nice. This confirms that:

    • PHP SOAP Extension is easy to use. Two statements are enough to call a SOAP service.
    • You PHP SOAP Extension is installed correctly and working.
    • PHP SOAP Extension supports WSDL.
    • www.xmethods.net is doing a great job for offering this demonstration SOAP service.

    PHP SOAP Extension Functions for Client Applications

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

    • SoapClient->__construct() - constructs a new SoapClient object
    • SoapClient->__soapCall() - Calls a SOAP function
    • SoapClient->__getFunctions() - Returns list of SOAP functions
    • SoapClient->__getLastRequestHeaders() - Returns last SOAP request headers
    • SoapClient->__getLastRequest() - Returns last SOAP request
    • SoapClient->__getLastResponseHeaders() - Returns last SOAP response headers
    • SoapClient->__getLastResponse() - Returns last SOAP response
    • ...

    SoapClient->__construct() allows you to construct a new SoapClient 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:

       'location'     => "...", # the URL where to send the request
       'uri'          => "...", # the name space of the SOAP service
       'soap_version' => SOAP_1_1 |SOAP_1_2,
       'trace'        => 0 | 1,
       ... 
    

    Note that SoapClient 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, 'location'
          # and 'uri' are required options. 
    

    SoapClient->__getFunctions() allows you to get a list of functions supported by the target node. This function is only valid in WSDL mode:

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

    SoapClient->__soapCall() allows you to make a RPC function call on the target SOAP node.

       $obj->__soapCall(string func_name[, array arguments [, ...]]);
    

    Note that in WSDL mode, you can also make a RPC call as a local method on the SoapClient object:

       $obj->func_name(arg1, arg2, ...);
    

    SoapClient->__getLastRequestHeaders() allows you to retrieve the HTTP request header lines of the last SOAP request executed. This function works only if the SoapClient object was created with the option of "trace=1".

       string $s = $obj->__getLastRequestHeaders();
    

    SoapClient->__getLastRequest() allows you to retrieve SOAP request message of the last SOAP request executed. This function works only if the SoapClient object was created with the option of "trace=1".

       string $s = $obj->__getLastRequest();
    

    SoapClient->__getLastResponseHeaders() allows you to retrieve the HTTP response header lines of the last SOAP request executed. This function works only if the SoapClient object was created with the option of "trace=1".

       string $s = $obj->__getLastRequestHeaders();
    

    SoapClient->__getLastResponse() allows you to retrieve SOAP response message of the last SOAP request executed. This function works only if the SoapClient object was created with the option of "trace=1".

       string $s = $obj->__getLastResponse();
    

    GetTempDump.php - Dumping Debugging Information

    After learning the basic functions of the SoapClient class, I rewrote the GetTemp.php client program to get more information on how SOAP Extension works, and to show you how to debug information. The new client program is called, GetTempDump.php:

    <?php # GetTempDump.php
    # Copyright (c) 2005 by Dr. Herong Yang
    #
       $zip = "123456";
       $client = new SoapClient
          ("http://www.xmethods.net/sd/2001/DemoTemperatureService.wsdl",
          array('trace' => 1));
    
       echo("\nDumping client object:\n");
       var_dump($client);
    
       echo("\nDumping client object functions:\n");
       var_dump($client->__getFunctions());
    
       $return = $client->getTemp($zip);
       echo("\nReturning value of getTemp() call: ".$return);
    
       $return = $client->__soapCall("getTemp",array($zip));
       echo("\nReturning value of __soapCall() call: ".$return);
    
       echo("\nDumping request headers:\n" 
          .$client->__getLastRequestHeaders());
    
       echo("\nDumping request:\n".$client->__getLastRequest());
    
       echo("\nDumping response headers:\n"
          .$client->__getLastResponseHeaders());
    
       echo("\nDumping response:\n".$client->__getLastResponse());
    ?>
    

    Output of this program:

    Dumping client object:
    object(SoapClient)#1 (3) {
      ["trace"]=>
      int(1)
      ["_soap_version"]=>
      int(1)
      ["sdl"]=>
      resource(5) of type (Unknown)
    }
    
    Dumping client object functions:
    array(1) {
      [0]=>
      string(30) "float getTemp(string $zipcode)"
    }
    
    Returning value of getTemp() call: 52
    
    Returning value of __soapCall() call: 52
    
    Dumping request headers:
    POST /soap/servlet/rpcrouter HTTP/1.1
    Host: services.xmethods.net
    Connection: Keep-Alive
    User-Agent: PHP SOAP 0.1
    Content-Type: text/xml; charset=utf-8
    SOAPAction: ""
    Content-Length: 510
    Cookie: JSESSIONID=uuqkGDvtzw_IPlMLsodnVX9j;
    
    Dumping request:
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope 
     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
     xmlns:ns1="urn:xmethods-Temperature-Demo" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
     SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
     <SOAP-ENV:Body><ns1:getTemp>
      <zipcode xsi:type="xsd:string">123456</zipcode>
     </ns1:getTemp></SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    
    Dumping response headers:
    HTTP/1.1 200 OK
    Server: Enhydra-MultiServer/3.1.1b1
    Status: 200
    Content-Type: text/xml; charset=utf-8
    Servlet-Engine: Enhydra Application Server/3.1.1b1 (JSP 1.1; 
       Servlet 2.2; Java 1.4.2_03; Linux 2.4.7-10smp i386; 
       java.vendor=Sun Microsystems Inc.)
    Content-Length: 470
    X-Cache: MISS from www.xmethods.net
    Keep-Alive: timeout=15, max=99
    Connection: Keep-Alive
    
    Dumping response:
    <?xml version='1.0' encoding='UTF-8'?>
    <SOAP-ENV:Envelope 
     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Body>
    <ns1:getTempResponse xmlns:ns1="urn:xmethods-Temperature-Demo" 
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <return xsi:type="xsd:float">52.0</return>
    </ns1:getTempResponse>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    The output is very useful. It confirms that:

    • The default SOAP version is SOAP 1.1. For SOAP 1.2, the envelope namespace should be http://www.w3.org/2003/05/soap-envelope, not http://schemas.xmlsoap.org/soap/envelope/.
    • The transportation protocol is HTTP/1.1. See the request header lines.
    • There is only one RPC function supported in this WSDL: "float getTemp(string $zipcode)". See the __getFunctions() dump.
    • SOAP Extension converts my getTemp() RPC call nicely in a SOAP request message based on the definitions in the WSDL document.
    • The returning value is also converted properly into a "float" type of value, not a "string". The SOAP response message shows "52.0", but the print out of $return is "52".

    Whis Is WSDL?

    WSDL (Web Services Definition Language): An XML based standard designed to describes protocol bindings and message formats of Web services. WSDL is often pronounced as "Whiz-Dull".

    A WSDL document is an XML document written in WSDL to describe Web service. Here is a copy of the WSDL document for the demonstration Web service used in previous sections. You can download one yourself by going to http://www.xmethods.net/sd/2001/DemoTemperatureService.wsdl with your Web browser:

    <?xml version="1.0"?>
    <definitions name="TemperatureService" 
     targetNamespace="http://www.xmethods.net/sd/TemperatureService.wsdl"
     xmlns:tns="http://www.xmethods.net/sd/TemperatureService.wsdl"   
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
     xmlns="http://schemas.xmlsoap.org/wsdl/">
     <message name="getTempRequest">
      <part name="zipcode" type="xsd:string"/>
     </message>
     <message name="getTempResponse">
      <part name="return" type="xsd:float"/>
     </message>
     <portType name="TemperaturePortType">
      <operation name="getTemp">
       <input message="tns:getTempRequest"/>
       <output message="tns:getTempResponse"/>
      </operation>
     </portType>
     <binding name="TemperatureBinding" type="tns:TemperaturePortType">
      <soap:binding style="rpc"
       transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="getTemp">
       <soap:operation soapAction=""/>
       <input>
        <soap:body use="encoded"
         namespace="urn:xmethods-Temperature-Demo"
         encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
       </input>
       <output>
        <soap:body use="encoded"
         namespace="urn:xmethods-Temperature-Demo"
         encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
       </output>
      </operation>
     </binding>
     <service name="TemperatureService">
      <documentation>Returns current temperature in a given U.S. zipcode
      </documentation>
      <port name="TemperaturePort" binding="tns:TemperatureBinding">
       <soap:address
      location="http://services.xmethods.net:80/soap/servlet/rpcrouter"/>
      </port>
     </service>
    </definitions>
    

    I cannot read this document well before learning the WSDL specifications. But it seems to be describing precisely how this Web service should be used.

    Using SOAP Extension in non-WDSL Mode

    I think we had enough fun with the WSDL mode. Let's try the non-WSDL mode now. Here is the third version of my getTemp SOAP client program, GetTempNonWsdl.php:

    <?php # GetTempNonWsdl.php
    # Copyright (c) 2005 by Dr. Herong Yang
    #
       $zip = "123456";
       $client = new SoapClient(null, array(
          'location' => 
             "http://services.xmethods.net:80/soap/servlet/rpcrouter",
          'uri'      => "urn:xmethods-Temperature-Demo", 
          'trace'    => 1 ));
    
       echo("\nDumping client object:\n");
       var_dump($client);
    
       $return = $client->__soapCall("getTemp",array($zip));
       echo("\nReturning value of __soapCall() call: ".$return);
    
       echo("\nDumping request headers:\n" 
          .$client->__getLastRequestHeaders());
    
       echo("\nDumping request:\n".$client->__getLastRequest());
    
       echo("\nDumping response headers:\n"
          .$client->__getLastResponseHeaders());
    
       echo("\nDumping response:\n".$client->__getLastResponse());
    ?>
    

    Here is output:

    Dumping client object:
    object(SoapClient)#1 (4) {
      ["uri"]=>
      string(29) "urn:xmethods-Temperature-Demo"
      ["location"]=>
      string(54) "http://services.xmethods.net:80/soap/servlet/rpcrouter"
      ["trace"]=>
      int(1)
      ["_soap_version"]=>
      int(1)
    }
    
    Returning value of __soapCall() call: 52
    
    Dumping request headers:
    POST /soap/servlet/rpcrouter HTTP/1.1
    Host: services.xmethods.net
    Connection: Keep-Alive
    User-Agent: PHP SOAP 0.1
    Content-Type: text/xml; charset=utf-8
    SOAPAction: "urn:xmethods-Temperature-Demo#getTemp"
    Content-Length: 508
    
    Dumping request:
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope
     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
     xmlns:ns1="urn:xmethods-Temperature-Demo" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
     SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
     <SOAP-ENV:Body><ns1:getTemp>
      <param0 xsi:type="xsd:string">123456</param0>
     </ns1:getTemp></SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    
    Dumping response headers:
    HTTP/1.1 200 OK
    Date: Wed, 05 Oct 2005 02:02:46 GMT
    Server: Enhydra-MultiServer/3.1.1b1
    Status: 200
    Content-Type: text/xml; charset=utf-8
    Servlet-Engine: Enhydra Application Server/3.1.1b1 (JSP 1.1;
       Servlet 2.2; Java 1.4.2_03; Linux 2.4.7-10smp i386;
       java.vendor=Sun Microsystems Inc.)
    Content-Length: 470
    Set-Cookie: JSESSIONID=RTiE9NZhFiqCdnPB36zgsXMi;Path=/soap
    X-Cache: MISS from www.xmethods.net
    Keep-Alive: timeout=15, max=100
    Connection: Keep-Alive
    
    Dumping response:
    <?xml version='1.0' encoding='UTF-8'?>
    <SOAP-ENV:Envelope
     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Body>
    <ns1:getTempResponse
     xmlns:ns1="urn:xmethods-Temperature-Demo"
     SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <return xsi:type="xsd:float">52.0</return>
    </ns1:getTempResponse>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    Output shows that:

    • My GetTempNonWsdl.php works the same as the WSDL mode version.
    • If you compare this output with the WSDL output, SOAP Extension generates the request headers with one difference: SOAPAction is not blank any more. It has the value of "urn:xmethods-Temperature-Demo#getTemp".
    • SOAP Extension also generates the request message differently in non-WSDL mode. The input parameter is provided with an element named as "param0". In the WSDL version, that element is named as "zipcode".

    Conclusion

    SOAP Extension supports SOAP 1.1 by default. I don't know how it works with SOAP 1.2. Needs to test it out.

    WSDL document does help simplifying the set up process of SOAP calls. But without WSDL documents, making SOAP calls is not that hard.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (8)

    New

    Hot