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

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

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:complexType>
          <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="a" type="xs:int"/>
            <xs:element minOccurs="1" maxOccurs="1" name="b" type="xs:int"/>
          </xs:sequence>
        </xs:complexType>

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.

<?php
  // 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",
    array(
      "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>";
?>

16 Responses to “Soap Client calling .NET Web service”

  1. Renars says:

    Helped me to solve the result problem, because of cache_wsdl flag. Thanks! Good post

  2. Andrea says:

    GREAT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  3. Andrew says:

    Very helpful. Thanks for sharing.

  4. Mario says:

    Man, you saved my life with the array of parameters.
    Thank you!

  5. naveen arora says:

    thanks for sharing. it solved my problem, i am looking for this.

  6. michele says:

    hi!

    very clear post!

    but what about Authentications methods?

  7. genux says:

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

  8. soap mas says:

    not working

  9. genux says:

    Why ? just wondering which part.

  10. rachka says:

    Helped me to solve the problem.
    Thanks!

  11. Yusuf says:

    but how can i get data which is posted to .net and results as an xml . i want top use the result in php variable.

  12. Rajan says:

    Your post solve my all problem . Thank you very much

  13. Max says:

    Great. Thank you very much. You saved my day. Helped me to solve my problem.

  14. Mallesh says:

    Great Article, it worked for me.

  15. Andri irwanto says:

    thanks..

  16. Mikaƫl says:

    If you don’t know how to easily use the SoapClient, go to http://www.wsdltophp.com and it’ll generate PHP classes so you master the calls very easily.

Leave a Reply