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>"; ?> |
Helped me to solve the result problem, because of cache_wsdl flag. Thanks! Good post
GREAT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Very helpful. Thanks for sharing.
Man, you saved my life with the array of parameters.
Thank you!
thanks for sharing. it solved my problem, i am looking for this.
hi!
very clear post!
but what about Authentications methods?
Thanks very much for everyone’s comments :). Shall contact Michele about the authentications methods
not working
Why ? just wondering which part.
Helped me to solve the problem.
Thanks!
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.
Your post solve my all problem . Thank you very much
Great. Thank you very much. You saved my day. Helped me to solve my problem.
Great Article, it worked for me.
thanks..
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.
Great.
Thank you very much.
Please how to send wsse header with name and password ?
Many thanks, This great article and helpful for me.
FINALLY! THANKS!!!!!!!!!!
I am facing an issue where soapClient is converting & to & Please assist.
Thanks
Thanks for nice tutorial. Was looking for something like this. Thanks a lot. Was easy to understand with just basic understanding.
Hi, first of all thank you for the great example.
I did exactly what you explained but in some cases the response string is truncated 🙁
Can You help me with that is there a way to find out how to manage this error?
Thank You
Flavio
Hi Flavio
When you say the response string is truncated, do you mean empty ?
Then just need to check the response with
is_null(the response)
or
if (the response == “”)
sort of thing before actually using it within the code.