PHP
  Home arrow PHP arrow Developing SOAP Clients using PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
PHP

Developing SOAP Clients using PHP
By: Mamun Zaman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2007-08-14


    Table of Contents:
  • Developing SOAP Clients using PHP
  • Debugging NuSOAP
  • PEAR::SOAP
  • PHP's SOAP Extension

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Developing SOAP Clients using PHP
    ( Page 1 of 4 )

    SOAP (Simple Object Access Protocol) provides a flexible communication layer between applications, regardless of platform and location. As long as both the server and the client speak SOAP, they can communicate. A PHP-based web application can ask a Java database application to get some information. In this article we will try to focus on different methods of developing SOAP web service clients in PHP.

    Prior to version 5, PHP did not have any support for SOAP. The major SOAP implementations for PHP are PEAR::SOAP and NuSOAP. Both of these are implemented using PHP, so you do not need any other libraries or binaries to use PEAR::SOAP or NuSOAP. Just add PHP classes for these implementations and you are ready to go. First we will use NuSOAP for the SOAP client, then we will describe PEAR::SOAP, and finally we will discuss the PHP5 SOAP Extension.

    NuSOAP is provided by NuSphere and Dietrich Ayala. It is a set of PHP classes that allow developers to create and consume web services based on SOAP 1.1, WSDL 1.1 and HTTP 1.0/1.1. This project is hosted in SourceForge at http://sourceforge.net/projects/nusoap/ . You can download the latest version from the above link and copy the nusoap.php file into your web directory. Afterward you can include this PHP file in your code. I placed it in the same directory as the sample code itself.

    Let us start with a simple server. The server exposes a single SOAP method named add, which takes two integer parameters for input and returns an integer.

    <?php
    require_once('nusoap.php');

    $server = new soap_server;

    $server->register('add');

    function add($a, $b) {
        return $a+$b;
    }

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';

    $server->service($HTTP_RAW_POST_DATA);
    ?>

    First we included nusoap.php, then created the soap server object and registered the function 'add'. Then we defined the function add. Let us save this server code as add.php. The client for this service is below.

    <?php
    require_once('nusoap.php');

    $client = new soapclient('http://localhost/add.php');

    $result = $client->call('add', array('a' => 5, 'b' => 7));

    print_r($result);
    ?>

    First we include the nusoap.php. Then we create a client using the soapclient function. The parameter of the soapclient function is the URL to the service. Then we use the call method of the soapclient object with the function name 'add' as the first parameter, and an array consists of the parameters of the function add as the second parameter. The output is then printed recursively using the print_r function. This is an example of the simplest SOAP client.

    NOTE: In the PHP5 SOAP extension, the function name for creating a SOAP client is also soapclient. So, if you want to use NuSOAP with PHP5, you need to rename the soapclient function of nusoap.php to something else. Otherwise you will get an error.

    We did not check for any error cases in the above example code. We can use the getError function of the soapclient object to get construction errors, as shown below.

    <?php
    require_once('nusoap.php');

    $client = new soapclient('http://localhost/add.php');

    $error = $client->getError();
    if($error){
         echo "Error occurred during construction!";
    }
    else{
         $result = $client->call('add', array('a' => 5, 'b' => 7));

         print_r($result);
    }
    ?>



     
     
    >>> More PHP Articles          >>> More By Mamun Zaman
     

       

    PHP ARTICLES

    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek