Home > PHP > PHP + NuSoap: A simple webservice

PHP + NuSoap: A simple webservice

December 20th, 2008

Have you ever needed to build a webservice?
After some googling i found a library called nusoap, full compatible with PHP4 and PHP5, very simple and intuitive.
Let me illustrate a very simple example of use this library for building webservices in PHP.

MyWebService.php
  1.  
  2. $namespace = "http://foo.bar.com/";
  3. require("lib/nusoap.php");
  4. $server = new soap_server();
  5. $server->debug_flag = false;
  6. $server->configureWSDL("MyWebService", $namespace);
  7. $server->wsdl->schemaTargetNamespace = $namespace;
  8.  
  9. // method name to expose
  10. $server->register('myMethod',
  11.     // parameters
  12.     array('param1' => 'xsd:string', 'param2' => 'xsd:string'),
  13.     // return value
  14.     array('return' => 'xsd:string'),
  15.     // namespace
  16.     $namespace,
  17.     // soapaction
  18.     $namespace . '#myMethod',
  19.     // style
  20.     'rpc',
  21.     // use
  22.     'encoded',
  23.     // method description
  24.     'Concatenate two strings'
  25. );
  26.  
  27. function myMethod($param1, $param2){
  28.     return $param1.$param2;
  29. }
  30.  
  31. $HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])?
  32. $GLOBALS['HTTP_RAW_POST_DATA'] : '';
  33. $server->service($HTTP_RAW_POST_DATA);
  34. exit();

With this few lines of code, we made a webservice, callable at this url:

http://foo.bar.com/MyWebService.php

This exposes a method MyMethod that takes two string parameters and concatenate them.

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon

PHP , , ,

  1. April 16th, 2009 at 04:22 | #1

    Last year for a project involving travelport xml api integration, we badly needed the soap requests to be gzipped, since the technical support people suggested.

    http://www.php-trivandrum.org/tips/gzip-nusoap-requests.html

  1. No trackbacks yet.