There are other guides on the net that focuse on NuSOAP, like this one on Sanity Free Coding, but I prefer to use the stock SOAP library in PHP5 so that's the focus for this guide.
The server: WSDL or non-WSDL-mode?
As of now, the PHP5 SOAP extension does not support auto-generating WSDL as the .Net Framework does. But it does support a non-WSDL mode. That creates an issue though, because Visual Studio relies on WSDL files in order to create the client reference as illustrated below.To make a long story short, I never got non-WSDL mode to work with .NET although I got pretty close, but then I relied on a dummy WSDL on the development computer just to fool Visual Studio into creating the proper service reference. So I needed the WSDL anyway.
If you're not proficient in WSDL-hacking, do what I did: Create a dummy .NET Web Service project with the method shells as intended for the PHP server. In my example here I'm going to create a simple service with one input and one output variable. The method TimesTwo doubles the integer input value and returns the result. In C# the code looks like this:
[WebService(Namespace = "http://myservice.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] public class SomeService : System.Web.Services.WebService { [WebMethod] public int TimesTwo(int value) { return 0; } }This I implemented in my dummy web service project. I have not implemented the business logic in the body - I only do as much as I need in order for the service to compile. I then get the WSDL by launching the app and adding ?WSDL to the URL. In my case, this became http://localhost:7048/SomeService.asmx?WSDL. Save this file for later use at the PHP server.
The Client
Now over to the client. I'm creating a simple Windows Forms app which takes an input, allows the user to trigger the web service call and displays the output. See screenshot below.Add a service reference to the solution by pointing to the local dummy project. It would be a good idea to also test the client in this phase and see that the dummy server returns the value zero. The client is really the easy part, so I'm moving over to the juice.
The PHP server
I'm not going into detail on installation and configuration. Consult the PHP SOAP manual for this.Now make sure you upload the WSDL file to the server. Let's assume you call it "SomeService.wsdl" and place it in the same folder as the server file itself. This is the code you need to provide the desired functionality:<?php ini_set("soap.wsdl_cache_enabled", "0"); $server = new SoapServer("SomeService.wsdl"); $server->addFunction("TimesTwo"); $server->handle(); function TimesTwo($valueObj) { // Unwrap input data $valueArr = get_object_vars($valueObj); $value = $valueArr["value"]; // Perform business operations $result = $value * 2; // Wrap and return the result return array("TimesTwoResult" => $result); } ?>
Now the tricky part was understanding how .NET wrapped the input data and how it expected the return data to be wrapped. Some Googling lead me to the answer on the return value. A bit of trial and error and with aid of the serialize() function, I figured out the input data. Note that the string literal ("TimesTwoResult") on the return line needs to to match the result specification in the WSDL. Similar, the "value" property in the input object is the name of the first input parameter.
Now that's all there is to it!
Download source code.
Awesome post!
ReplyDeletei'm noob in c#, maybe you ask me? I'm recived array data in the soap envelop, but string s1 = client.TimesTwo("foo");
ReplyDeletestring input = InputTextBox.Text;
ResultTextBox.Text = s1;
- -------this code snipet return just first element of soap complex type array-----
How can i fetch all elements? for example 10 items?
Thak you
This comment has been removed by the author.
ReplyDeleteSorry for late answer. How is your PHP code implemented? Are you saying you are returning an array of String's? If so, try changing the method signatur in C# to "String[]" or "List<String<"
DeleteHey! Finally we are launch 150+ High DA Dofollow Quality Backlinks here! Order Now and Boost your website ranking.
ReplyDeleteManual Backlinks | Quality Backlinks | Dofollow Backlinks | High Quality Backlinks
Thank you!
DigiPeek
Shield Security Solutions Offers Security Guard License Training across the province of Ontario. Get Started Today!
ReplyDeleteSecurity Guard License | Security License | Ontario Security license | Security License Ontario | Ontario Security Guard License | Security Guard License Ontario
شركة تنظيف بابها
ReplyDeleteشركة تنظيف مجالس وكنب بابها
شركة تنظيف سجاد وموكيت بابها
شركة مكافحة حشرات بابها
شركة تنظيف خزانات المياه بابها
شركة جلي سراميك و رخام بابها
شركة مكافحة الصراصير بابها
شركة تنظيف بخميس مشيط
ReplyDeleteوايت شفط الصرف الصحي
شركة تنظيف مجالس بخميس مشيط
شركة تنظيف سجاد وموكيت بخميس مشيط
شركة مكافحة حشرات بخميس مشيط
شركة تنظيف خزانات المياه بخميس مشيط
شركة جلى رخام وسيراميك بخميس مشيط
شركة مكافحة الصراصير بخميس مشيط
Thank you for your solution. Quikads
ReplyDelete