{"id":886,"date":"2010-04-16T10:01:00","date_gmt":"2010-04-16T09:01:00","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=886"},"modified":"2010-04-16T10:20:43","modified_gmt":"2010-04-16T09:20:43","slug":"soap-client-calling-net-web-service","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/16\/soap-client-calling-net-web-service\/","title":{"rendered":"Soap Client calling .NET Web service"},"content":{"rendered":"<p>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 (<a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/15\/web-service\/\">Web Service<\/a>).<\/p>\n<p>As before with the soap client for a PHP server (<a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/12\/soap-server\/\">PHP server previous post<\/a>) we just need to link to the WSDL output of the .NET web service as below<\/p>\n<pre lang=\"php\">\r\n$client = new SoapClient(\"http:\/\/localhost\/csharp\/web_service.asmx?wsdl\")\r\n<\/pre>\n<p>so if you tried to call the function \/ web method.<\/p>\n<pre lang=\"php\">\r\necho $client->Add(2,3);\r\n<\/pre>\n<p>The error would be <\/p>\n<pre lang=\"html\">\r\nCatchable fatal error: Object of class stdClass could not be converted to string in \r\n<\/pre>\n<p>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<\/p>\n<pre lang=\"php\">\r\nprint_r($client->Add(2,3));\r\n<\/pre>\n<p>the output would be<\/p>\n<pre lang=\"html\">\r\nstdClass Object ( [AddResult] => 0 ) \r\n<\/pre>\n<p>thus to gain access to the actual result you would need to reference the first part of the returned array named AddResult<\/p>\n<pre lang=\"php\">\r\necho $client->Add(2,3)->AddResult;\r\n<\/pre>\n<p>but the parameters being passed are not!!, so if you look at the WSDL file generated from the .NET web service it has <\/p>\n<pre lang=\"xml\">\r\n      <xs:element name=\"Add\">\r\n        <xs:complexType>\r\n          <xs:sequence>\r\n            <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"a\" type=\"xs:int\"\/>\r\n            <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"b\" type=\"xs:int\"\/>\r\n          <\/xs:sequence>\r\n        <\/xs:complexType>\r\n<\/pre>\n<p>complex types and thus if you try to pass in parameters to the function Add (as the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/15\/web-service-the-consumer-client\/\">web service client<\/a> 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 <\/p>\n<pre lang=\"php\">\r\nprint_r( $client->Add(array(\"a\" => \"5\", \"b\" =>\"2\")));\r\n<\/pre>\n<p>which displays a better result for us..<\/p>\n<pre lang=\"html\">\r\nstdClass Object ( [AddResult] => 7 ) \r\n<\/pre>\n<p>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)<\/p>\n<pre lang=\"php\">\r\necho $client->Add(array(\"a\" => \"5\", \"b\" => \"2\"))->AddResult;\r\n<\/pre>\n<p>So here is the full code to pull back the code with some debugging information.<\/p>\n<pre lang=\"php\">\r\n<?php\r\n  \/\/ create a connection to the local host mono .NET pull back the wsdl to get the functions names\r\n  \/\/ and also the parameters and return values\r\n  $client = new SoapClient(\"http:\/\/localhost\/csharp\/web_service.asmx?wsdl\",\r\n    array(\r\n      \"trace\"      => 1,\t\t\/\/ enable trace to view what is happening\r\n      \"exceptions\" => 0,\t\t\/\/ disable exceptions\r\n      \"cache_wsdl\" => 0) \t\t\/\/ disable any caching on the wsdl, encase you alter the wsdl server\r\n  );\r\n\r\n  \/\/ get a response from the WSDL zend server function getQuote for the day monday\r\n  print_r( $client->Add(array(\"a\" => \"5\", \"b\" =>\"2\")));\r\n\r\n  \/\/ display what was sent to the server (the request)\r\n  echo \"<p>Request :\".htmlspecialchars($client->__getLastRequest()) .\"<\/p>\";\r\n  \/\/ display the response from the server\r\n  echo \"<p>Response:\".htmlspecialchars($client->__getLastResponse()).\"<\/p>\";\r\n?><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/16\/soap-client-calling-net-web-service\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Soap Client calling .NET Web service<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-886","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=886"}],"version-history":[{"count":4,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/886\/revisions"}],"predecessor-version":[{"id":890,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/886\/revisions\/890"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}