View Full Version : SOAP or no SOAP
11-25-2011, 12:11 AM
So I have this client, I love clients at times. Wanted an API, web API to make communication between X program and another X program on another server in the same building. So after 3 meetings with this client I told them yes, I could make a PHP app that would accept POST call's and send data back in XML format by simply echoing data in XML format back (since they said the returned data had to be in XML). So I thought easy enough yeah sure.
I told them exactly 100% how I would do it, they gave me the requests they would be making to the API and what needed to be returned. Simple, until after they accepted the bid and I asked, "How are you going to be making these requests; PUT, POST or GET?".
Bad questions to ask, there initial software is in .NET, so they proceeded to tell me, well none of those (acting really pretentious towards me in this conversation ha ha). We REQUIRE that your application be WSDL and SOAP communication and return us an XML FILE! All pretentious and obnoxious like ha ha
Now, what the heck is that?? I have googled SOAP and know PHP has an interpreter for it, but have never used it. But on the WSDL page I couldn't really understand what the heck that was ha ha
I really want to say the heck with it and find another programmer since I told them originally how I would do it and it's not my fault they don't listen.
Could I not just tell them, yeah sure, and do it my original way and just echo out XML format in the way they want it an they will never know the difference? (I am asking this since I don't know what the heck WSDL is and how it works with SOAP or why they would want SOAP anyways if they can just program in an XML parser and send post requests to my file ha ha)
They told me they would make their requests to my file this way:
objeto = new MSSOAP.SoapClient30
objeto.mssoapinit("http://ip/servicio.wsdl", "", "")
xml_respuesa = objeto.metodo(xml_parametros)
Or do I just throw in the towel because they are pretentious jerks ha ha ha.
Thanks for any advise.
11-25-2011, 03:00 AM
SOAP itself is easy as pie. The problem is always PHP's datatype weakness which tends to grind some things you'd expect to be easy to a halt :/
WSDL generation itself is quite complicated. I'm willing to bet there are generators out there to write it for you though. Basically they explain what the simple object is, and how you talk to it.
Looks to me like you have a WSDL already available? That can simply be used to open a SOAPClient (or look at using nusoap, I just use the builtin since they are easy enough), and then you kinda treat it like a PHP object. Yes it has to be object oriented though (although you can certainly write procedural methods to treat it this way, the communication with the builtin soapclient and soapserver are both object oriented).
PHP is almost identical to the above:
$client = new SOAPClient('http://ip/wsdl.wsdl');
$result = $client->__soapcall('metodo', array('parameters')); // This is chained to __call, which means you can just use $client->metodo().
Simple as that.
Server is similar, but backwards. You typically choose the wsdl that describes yourself, register the methods that handle all the input and output available, and then set a handling listener. Away it goes, clients would simply use soapclients.
As for XML output, SOAP itself is an XML format. Is that what they mean or do they actually want your results in XML? Both are easy enough, properly formatted HTML is really just a form of XML (if using single ended tags properly such as in XHTML and not in HTML, although schema/DTD dictates if you can do that).
Ultimately, the most difficult thing of SOAP generation is the creation of the WSDL, which you actually learn to do fairly quickly (and I assume generators exist; I built a reflector a few years ago that did just this task). Beyond this, its learning how to pass the parameters, and worst of all, figuring out what PHP did wrong with the exceptionally complex datatypes when it didn't properly create it as your desired type or stdclass.
Oh btw, since you kinda mentioned it. You can certainly handle soap in a manual fashion. You will be given an XML file of a soap envelope, and you can pick it a part to determine what it is that it wants, and handle your scripts accordingly. IMO, this is a lot more work to do than using a soapserver. Likewise, you can manufacture all of your soap requests from simple strings or XML, but it is also more difficult than the two lines I have above there.
Oh, I should mention that overall debugging is also a nightmare in soap. Reason being is that so many thing can catch you, and its difficult to identify the problem as either the client or server.
11-25-2011, 02:14 PM
Hmm, ok. No actually the WSDL doesn't exist, that was just their example of what they wanted. I have to create the WSDL file myself.
You mentioned SOAPServer, do I need a SOAP server app running on the machine with apache and PHP? Or was this just a term used for my PHP script?
As for XML output, SOAP itself is an XML format. Is that what they mean or do they actually want your results in XML?
From what I understand they want it in XML format, although their original email said they wanted an XML file, which doesn't make any sense to me. So I figure just XML format if they are calling on my file for each request.
11-25-2011, 02:21 PM
What I don't understand is this, if they make a call to the WSDL file (which they are doing in the above code), then how does my PHP script know they are calling it for information and return info? Or does the WSDL file redirect the call to a script I have defined in it somewhere?
Also for their incoming calls, do I just create a Class and then a function for each definition I create in the WSDL file? Then when that WSDL file calls my script it immediately calls that function which will echo (return) the information requested?
That's what has me boggled :S ha ha
11-25-2011, 03:58 PM
The way soap works is that the client (may, its not actually required but is more hit and miss) loads the WSDL file. The WSDL file tells the soap client where to connect to in order to invoke methods from the soap server. SOAPServer is simply a handling class in PHP, it runs just as any other script, but attaching directly will give unusual results if any at all.
The WSDL will specify the port binding and within it is the address of where to contact for the method to invoke - that's how it knows where to go.
The WSDL file itself does nothing. Its job is to tell the client (no matter what language it is, as long as its a soapclient) where to go to retrieve information from, and how to actually talk to the methods presented by the soapserver. Check this link for a quick example of a WSDL file: http://www.w3.org/2001/03/14-annotated-WSDL-examples
With an established soapclient and soapserver, the client now requests data from the soapserver via method calls. Its almost like running a simple:
$server = new MyClass(); // options or wsdl would be here, but this is just 'MyClass'.
$fQuote = $server->getLastTradePrice('AAPL');
printf('Apple\'s last trade price: %0.2f', $fQuote);
(for example from the second WSDL in the given link).
Personally I use __soapCall instead of the override for __call; the above would likely complain in my IDE if I use an actual soapclient, since it can't actually find a method called getLastTradePrice. Runtime would be fine.
The only difference really is that the soapclient is a remote access to a method, while the include is a local one. Client wise, it doesn't really see a difference.
11-25-2011, 07:49 PM
Aha, ok got it, so basically the WSDL is a definition file, or better yet a class Map to the different functions for each client to know what it can and cannot do. Got it, thanks Fou
11-25-2011, 08:09 PM
Definition file is the best description of it. WSDL = Web Services Definition Language