Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - Using SOAP with PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Using SOAP with PHP
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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


       · Apparently, I live in a cave, but I did not know exactly what SOAP did prior to...
       · SOAP (and XML-RPC) are for doing remote procedure calls. Effectively, you call a...
       · Exactly. Pass off the values and get back values, but the function doing the actual...
       · Hello,this tutorial is very interesting and gives a good quickstart about SOAP...
       · Great tutorial Matt, the one thing I would recommend it a link to where a person...
       · Or I could follow along better :) Thanks for this.Laura
       · Not getting a value back from the server?Did you remember to change the...
       · I too am not getting a response for $stockprice.this is the $c-&gt;response...
       · I'm just intrested to know... can you view your WSDL from the server page?I had...
       · Hello,Im getting this error msg,Im using PHP5.Can anyone help me...
       · Hello,Im getting this error msg,Im using PHP5.Can anyone help me...
       · how to send parameter with more than one parameter...
       · yes that article unfortunatly don't give real example of real life of XML and SOAP...
       · hiya I am trying to get this soap example working to provide a est bed for a soap...
       · I think the problem may be with php5 soap class you will need to redefine the class...
       · i looked @ it and found if u change it to $c = new nusoap_client it will...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT