Soap Client calling .NET Web service

Since Web Service Description Language (WSDL) is to be used between language so that a PHP program can call a .NET web service or the other way around. So here we create a PHP Soap Client that will call the .Net Web service created from a previous post (Web Service).

As before with the soap client for a PHP server (PHP server previous post) we just need to link to the WSDL output of the .NET web service as below

$client = new SoapClient("http://localhost/csharp/web_service.asmx?wsdl")

so if you tried to call the function / web method.

echo $client->Add(2,3);

The error would be

Catchable fatal error: Object of class stdClass could not be converted to string in

this does not mean that much or really point to where the problem is, the actual problem is not the passing parameters (even through they are in a complex format) but the return from the $client object, because .NET returns a array object as such, so if you


the output would be

stdClass Object ( [AddResult] => 0 )

thus to gain access to the actual result you would need to reference the first part of the returned array named AddResult

echo $client->Add(2,3)->AddResult;

but the parameters being passed are not!!, so if you look at the WSDL file generated from the .NET web service it has

      <xs:element name="Add">
            <xs:element minOccurs="1" maxOccurs="1" name="a" type="xs:int"/>
            <xs:element minOccurs="1" maxOccurs="1" name="b" type="xs:int"/>

complex types and thus if you try to pass in parameters to the function Add (as the web service client does) as a normal PHP way it is not passing in the numeric values from the php code, the reason for this PHP has to specify the actual names of the passing parameters within a array.. as

print_r( $client->Add(array("a" => "5", "b" =>"2")));

which displays a better result for us..

stdClass Object ( [AddResult] => 7 )

So the best way to remember is that if you are calling a .NET WSDL web service, then always pass in a array with the WSDL function parameters within a index array and the return result is always function name + Result (AddResult)

echo $client->Add(array("a" => "5", "b" => "2"))->AddResult;

So here is the full code to pull back the code with some debugging information.

  // create a connection to the local host mono .NET pull back the wsdl to get the functions names
  // and also the parameters and return values
  $client = new SoapClient("http://localhost/csharp/web_service.asmx?wsdl",
      "trace"      => 1,		// enable trace to view what is happening
      "exceptions" => 0,		// disable exceptions
      "cache_wsdl" => 0) 		// disable any caching on the wsdl, encase you alter the wsdl server
  // get a response from the WSDL zend server function getQuote for the day monday
  print_r( $client->Add(array("a" => "5", "b" =>"2")));
  // display what was sent to the server (the request)
  echo "<p>Request :".htmlspecialchars($client->__getLastRequest()) ."</p>";
  // display the response from the server
  echo "<p>Response:".htmlspecialchars($client->__getLastResponse())."</p>";

22 thoughts on “Soap Client calling .NET Web service”

  1. Thanks very much for everyone’s comments :). Shall contact Michele about the authentications methods

  2. Thanks for nice tutorial. Was looking for something like this. Thanks a lot. Was easy to understand with just basic understanding.

Leave a Reply

Your email address will not be published. Required fields are marked *