Programming web services with SOAP in different web programming languages (like ASP.NET and PHP) poses some challenges. The most subtle such challenge deals with a language’s attribute of being strongly typed or not. While Java and C# are typed languages, in PHP typing is not enforced. Thus, the same variable can be reused as a string despite being declared as an integer number.

Since SOAP offers a specification for communication that strives to be impartial, naming and typing must be resolved at language level.

In my case, I will take a simple ASP.NET web service example.

   1:  <%@ WebService Language="C#" Class="WebService.HelloWorldService" %>
   2:  using System.Web.Services;
   3:  namespace WebService
   4:  {
   5:    public class HelloWorldService 
   6:    {
   7:      [WebMethod]
   8:      public string HelloWorld()
   9:      {
  10:        return "Hello World";
  11:      }
  12:      
  13:      [WebMethod]
  14:      public int AddInt(int a, int b)
  15:      {
  16:        return a + b;
  17:      }
  18:      
  19:      [WebMethod]
  20:      public float Add(float a, float b)
  21:      {
  22:          return a + b;
  23:      }
  24:    }
  25:  }

 

The client that will consume the web service, should be in PHP (using the Zend Framework, inside the library folder in the set_include_path call):

   1:  <?php
   2:  set_include_path(get_include_path() . PATH_SEPARATOR . realpath("../library/"));
   3:  ini_set("soap.wsdl_cache_enabled", "0");
   4:  require_once("Zend/Soap/Client.php");
   5:   
   6:  $url = 'http://127.0.0.1:8080/HelloWorld.asmx?wsdl';
   7:   
   8:  $service = new Zend_Soap_Client($url);
   9:  var_dump($service->AddInt(11,3));
  10:  ?>

However, calling the AddInt function in this manner will result in an output of 0. Why? Because the ASP.NET service expects also variable names.

The proper way of calling the function is this:

   1:  $service->AddInt(array("a"=>11,"b"=>3));

The essentials to remember when consuming an ASP.NET web service with PHP:

  • The result of calling the ASP.NET web service’s functions is a PHP StdClass object with one member variable name like this <function name>Result (eg: AddIntResult).
  • The parameters must be sent inside one array, where each item is labeled according to the names of the parameters the web service expects.
  • Test your ASP.NET or PHP web service using the SoapUI web service test tool.