PHP

Creating a Web Service with PEAR::SOAP

In PHP's official package repository PEAR, you can find a SOAP extension at http://pear.php.net/package/SOAP. It depends on several other packages, so you do need to install all that are mentioned on the download page. The current released version works under PHP 4, but the CVS system of PHP (browsable online via http://cvs.php.net/) contains a port for PHP 5. This tutorial uses the PHP 4 version that seems to be more tested and in use than the PHP 5 version from CVS. Use pear install f SOAP; you might get a list of packages that are required for PEAR::SOAP to run.

A Web Service with PEAR::SOAP

<?php
  error_reporting(E_ALL ^ E_NOTICE);
  require_once 'SOAP/Server.php';
  $soap = new SOAP_Server;
  $service = new ServiceClass();
  $soap->addObjectMap($service, 'urn:php-hoshmand-
    soapservice');
  $soap->service($HTTP_RAW_POST_DATA);
  class ServiceClass {
    function add($a, $b) {
      return $a + $b;
    }
  }
?>

Providing a Web Service is again a matter of a few lines of code: Instantiate a class (SOAP_Server), and then attach the business logic to it (this time, by putting it into a class and then instantiating the class). Finally, call addObjectMap() to provide a uniform resource name (URN) for the method and then connect it with the method service() to $HTTP_RAW_POST_DATA. This code contains all the details, so you can start from there for your own Web Service.

Unfortunately, PEAR::SOAP currently does not work when error_reporting is set to E_ALL because notices occur in the code. Therefore, the code in this tutorial sets the error reporting to a less severe value before loading the PEAR::SOAP classes.

by BrainBellupdated
Advertisement: