Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Using SOAP with PHP
MISCELLANEOUS

Using SOAP with PHP
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 69
    2004-06-22

    Table of Contents:
  • Using SOAP with PHP
  • Definitions
  • Define Our Goal
  • Create a SOAP server
  • The WSDL Document
  • Creating a SOAP Client
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    Using SOAP with PHP - Create a SOAP server


    (Page 4 of 7 )

    The first thing we need to do is to create the SOAP server. This is the script that will fetch the data from the database and then deliver it to the Client. One wonderful thing about the NuSOAP library is that this same Server script will also create a WSDL document for us.

    The first step is to create a function that will fetch the data we want. Create this function just as you would any other. It is just straight up PHP. The one trick is to name the function something sensible, as this will be the name that is used when the Client contacts the Server.

    <?php
    function getStockQuote($symbol) {

        
    mysql_connect('server','user','pass');
        
    mysql_select_db('test');
        
    $query "SELECT stock_price FROM stockprices "
               
    "WHERE stock_symbol = '$symbol'";
        
    $result mysql_query($query);

        
    $row mysql_fetch_assoc($result);
        return 
    $row['stock_price'];
    }
    ?>

    Now, it is time to turn this function into a Web Service. Basically, all we have to do is include the NuSOAP library, instantiate the soap_server class and then register the function with the server. Let's go through it step by step, after which I will present the completed script.

    The first thing necessary is to simply include the NuSOAP library.

    require('nusoap.php');

    Next, instantiate an instance of the soap_server class.

    $server = new soap_server();

    The next line is used to tell NuSOAP information for the WSDL document it is going to create for us. Specifically we specify the name of the server and the namespace, in that order.

    $server->configureWSDL('stockserver', 'urn:stockquote');

    Now, we register the function we created with the SOAP server. We pass several different parameters to the register method.

    The first is the name of the function we are registering.

    The next parameter specifies the input parameters to the function we are registering. Notice that it is an array. The keys of the array represent the names of the input parameters, while the value specifies the type of the input parameter. One thing that pure PHP programmers might find odd is that I had to specify what types my input and return parameters are with the designations of xsd:string and xsd:decimal. It is required that you describe your data properly. You are not dealing with a loosely typed language here.

    The third parameter to the register method specifies the return type of the registered function. As shown below, it is fashioned in the same way as the last parameter, as an array.

    The next two parameters specify the namespace we are operating in, and the SOAPAction. For more information on the SOAPAction see http://www.oreillynet.com/pub/wlg/2331.

    $server->register("getStockQuote",
                    array('symbol' => 'xsd:string'),
                    array('return' => 'xsd:decimal'),
                    'urn:stockquote',
                    'urn:stockquote#getStockQuote');

    Now, we finally finish it off with two more lines of code. The first simply checks if $HTTP_RAW_POST_DATA is initialized. If it is not, it initializes it with an empty string. The next line actually calls the service. The web request is passed to the service from the $HTTP_RAW_POST_DATA variable and all the magic behind the scenes takes place.

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
                          ? $HTTP_RAW_POST_DATA : '';
    $server->service($HTTP_RAW_POST_DATA);

    Here is the completed server script which I have saved in a file named stockserver.php.

    <?php
    function getStockQuote($symbol) {

        
    mysql_connect('server','user','pass');
        
    mysql_select_db('test');
        
    $query "SELECT stock_price FROM stockprices "
               
    "WHERE stock_symbol = '$symbol'";
        
    $result mysql_query($query);

        
    $row mysql_fetch_assoc($result);
        return 
    $row['stock_price'];
    }

    require(
    'nusoap.php');

    $server = new soap_server();

    $server->configureWSDL('stockserver''urn:stockquote');

    $server->register("getStockQuote",
                    array(
    'symbol' => 'xsd:string'),
                    array(
    'return' => 'xsd:decimal'),
                    
    'urn:stockquote',
                    
    'urn:stockquote#getStockQuote');

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
                          ? 
    $HTTP_RAW_POST_DATA '';
    $server->service($HTTP_RAW_POST_DATA);
    ?>

    More Miscellaneous Articles
    More By Matt Wade

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Attention: Forum and Site Maintenance
    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap