Web Services - Picking the right one for the right job
Picking your web service standard is an important decision and shouldn’t be taken lightly, and development teams shouldn’t be allowed to make the decision on their own.
Roughly there’s 3 “standards” to choose from; SOAP (Simple Object Access Protocol), XML-RPC, and “REST” (”REpresentational State Transfer”). It’s worth touching briefly on what each is before continuing;
- SOAP is very heavily pushed by IBM and Microsoft and has standard tooling within various IDEs and frameworks. This means for a lot of developers this is the only Web Service standard that they consider. It allows the Web Service to handle complex (and “rich” datatypes) both as parameters and for returning, and has a standard called WSDL that allows Web Services to be defined in a manner which is language agnostic (for those of you old enough; yes, this is very much like IDL and CORBA’s IDL) and is downloaded so the script can adapt things itself. Best of all this means that you can both pass objects and retrieve them without having to realise how the information is transferred, which for programmers is a massive boon. It works by encoding everything as XML and passing the XML as an encoded object to via HTTP GET, then the return values are passed back as XML, the SOAP framework does all the hard work of translating to and from XML on both sides.
- XML-RPC seemed like a reasonable competitor to SOAP, but seems to have diminished over time. It too allows “rich” and complex data to be passed to and returned from the Web Service.
- REST is the outsider, although there are toolkits and frameworks to help it relies on simplicity of parameter passing, although any data format can be passed back. This means it’s hard to pass complex data, and what it returns needs to be interpreted (rather than being magically converted into objects/structs that are usable within the calling language). It also frequently requires custom coding to create the web service, and most of that work will be manual.
So, you’re probably thinking, why would I go for anything but SOAP? It seems the best, most widely used and easy to develop web service technology, so surely it’s the only choice? The answer is unfortunately not obvious..
The answer is related to who you’re creating the Web Service for (your “target audience”) and what you want them to do with your data.
If you’re on an intranet and you need .NET and Java code bases to cross communicate then SOAP is an excellent way to go, development is quick, it’s easily secured and toolkits are in place for both platforms to allow you to create and consume (and debug) Web Services very easily.
In addition, if you’re an internet based app and you’re working with medium and large enterprises, then SOAP could again be a good fit, especially if you’re looking at clients that are based heavily around .NET and Java.
If, however you’ve got a wide development language base within your intranet (using scripting languages such as python, php, perl, ruby etc.) then it may not be a good fit. Scripting languages will need to execute more code before they can make the calls (and the calls may still have minor issues when they communicate with Web Services - despite the popular press SOAP still has issues and incompatibilities when things are getting complex) and you’ll need to start including additional libraries to support it.
REST really shines when you have either; a varied user base (you can’t check which languages consumers of the service will use), and/or some of the users will be small businesses or even single developers creating mash-ups. This is because it’s incredibly simple - parameters are either encoded as part of the path or on the query string and it returns whatever is most useful. If it makes sense to return CSV information from a REST Web Service then that’s just fine, JSON? yep fine again.
Because REST is based around a standard HTTP GET or POST call it’s easy to implement, even if you don’t have an easy way to open up a GET or POST call (for instance using cURL from just about any language - including shelling out to it if needed from UNIX shell scripts or MS-DOS bat files) you can custom code it - it’s trival even by creating a socket yourself and sending a GET HTTP request on port 80 (although i seriously doubt you’ll do that).
The other time you should seriously consider REST above the other choices is when you want to use the Web Service for more than backend server usage - for instance when you want javascript based AJAX to use the same information or you’re creating javascript based mashups. Ever tried building a SOAP client usable from javascript? it’s non-trivial and you’ll be fixing bugs from here to eternity.
So, let’s take a look at some code and see why SOAP is so attractive to developers and why REST is more difficult but more accessible. For the examples we’ll create a fake music based web service that allows users to find tracks that have been created by an artist.
For a SOAP example (in PHP)
/* create an instance of the web service proxy that you'll call on to - pass it the link to the WSDL file that tells the SOAP client what the web service needs as parameters and what it returns */
$musicfinder = SoapClient('http://someexample.com/webservices/musicfinder.wsdl')
/* use the proxy object and call the "findTracks" method and pass the first argument (in this case the artist) */
$tracks = $musicfinder->findTracks('madonna');
/* iterate and do whatever you need with them */
foreach($tracks as $track) {
print("TRACK IS : " . $track->trackName());
}
REST example (in PHP) version 1 - we’ll assume it returns XML, and we’ll use the verbose (non-wrappered) version of curl
function CallWebService($url) {
$ch = curl_init($url); $
response = curl_exec($ch);
//optionally you can check the response and see what the HTTP Code returned was
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_close($ch);
return $response;
}
//build the string to call it $url = "http://someexample.com/webservices/rest/findtracks/" .$artistname;
$resultsxml = CallWebService($url);
//load the dom - skipped here and set up an iteration etc.
REST example (in PHP) version 2 - we’ll assume it returns XML, and we’ll use the file_get_contents cheat method
function CallWebService($url) {
return file_get_contents($url); //may be disabled on some ISPs
}
//build the string to call it
$url = "http://someexample.com/webservices/rest/findtracks/" .$artistname;
$resultsxml = CallWebService($url); //load the dom - skipped here and set up an iteration etc.
REST example (in PHP) version 3 - we’ll assume it returns CSV, and we’ll use the file_get_contents cheat method again
function CallWebService($url) {
return file_get_contents($url); //may be disabled on some ISPs
}
//build the string to call it
$url = "http://someexample.com/webservices/rest/findtracks?artistname=" .$artistname;
$resultcsv = CallWebService($url);
foreach(split("\n", $resultcsv) as $line){
print("TRACK: " . $line[ COLUMN_CONTAINING_TRACK_NAME ]);
}
You can see it’s easy to use SOAP, but the REST call is very clear in the server logs, and it’s easy to construct the call and to process the call on the server side (read the query string parameters, do something and return something).
I know of one major company that has provided SOAP interfaces for it’s Web Services, without realising it’s core audience is single developers and small development houses.. almost all using scripting langauges very heavily (and most of them have poor programming skills), as a consequence it’s barely being used. The company keeps enhancing it’s interfaces to give more capabilties (I can only presume they think the issue is that they’re not providing enough functionality) but the user base just can’t use them.
There’s a reason why Google, Amazon, eBay etc. all provide REST interfaces - hopefully now you can get an idea why.
PS: If you’re trying hard to use PHP with SOAP then i’ll recommend this “Dirty Secrets” document with inside scoop with regards to using PHP with SOAP. Also, i’d ignore anything but the core SOAP library - they’re poorly maintained (although i’d love to be proved wrong), however sometimes it’s difficult to get the extension installed. In that case you’ll just have to try other libraries until it works.
December 9th, 2008 at 1:45 am
Огромное спасибо за потрясающие идеи!!! Буду следить за блогом, много всего интересного. А мой блог о науке, надеюсь, тоже понравится
March 8th, 2010 at 7:05 pm
bJ8wy8 Excellent article, I will take note. Many thanks for the story!
March 18th, 2010 at 6:28 am
Good post and this fill someone in on helped me alot in my college assignement. Say thank you you seeking your information.
April 24th, 2010 at 4:35 pm
Sorry for my bad english. Thank you so much for your good post. Your post helped me in my college assignment, If you can provide me more details please email me.
May 7th, 2010 at 10:06 am
fake watch
May 8th, 2010 at 3:12 am
replica watches
May 8th, 2010 at 5:01 am
replica watches
May 13th, 2010 at 5:40 pm
replica watch
May 22nd, 2010 at 1:48 pm
fake watches
May 24th, 2010 at 9:31 am
rolex submariner watches
May 26th, 2010 at 11:43 am
fake bvlgari
May 28th, 2010 at 4:23 am
Louis Vuitton Handbags
May 29th, 2010 at 8:12 am
air king watches
May 29th, 2010 at 8:23 am
day date watches
June 1st, 2010 at 2:18 am
Louis Vuitton
June 4th, 2010 at 4:40 am
audemars piguet
June 5th, 2010 at 10:42 am
daytona watches
June 5th, 2010 at 11:19 am
fake rado watches
June 7th, 2010 at 11:21 am
tag heuer watches
June 8th, 2010 at 2:22 am
piaget
June 8th, 2010 at 2:49 am
replica bell ross watches
June 8th, 2010 at 3:00 am
replica breitling watches
June 8th, 2010 at 3:11 am
chopard
June 8th, 2010 at 3:14 am
bvlgari
June 8th, 2010 at 3:35 am
fake watch
June 8th, 2010 at 3:42 am
fake breitling
June 8th, 2010 at 4:29 pm
breitling replica
June 9th, 2010 at 4:33 am
replica watches
June 9th, 2010 at 4:44 am
omega
June 9th, 2010 at 6:02 am
cartier watches
June 9th, 2010 at 6:53 am
cartier
June 10th, 2010 at 8:53 pm
rolex
June 19th, 2010 at 3:29 am
replica watches
July 2nd, 2010 at 3:47 am
replica watch
July 2nd, 2010 at 3:58 am
watchvisa
July 8th, 2010 at 3:44 am
fake watch
July 13th, 2010 at 2:23 am
Fake handbags
July 17th, 2010 at 11:54 am
fake rolex
July 17th, 2010 at 12:05 pm
fake tag heuer watches for sale
July 19th, 2010 at 1:15 am
chanel
July 23rd, 2010 at 2:01 am
replica rolex
July 23rd, 2010 at 2:06 am
rolex replica
July 27th, 2010 at 2:58 am
watches
July 28th, 2010 at 10:18 pm
replica watches
July 28th, 2010 at 10:30 pm
replica watch
July 31st, 2010 at 7:38 am
breitling
July 31st, 2010 at 7:48 am
tag heuer watches
July 31st, 2010 at 7:59 am
replica rolex watches