Long weekends reduce brain cells to jelly. Worked on a few things over the weekend, nothing really to report except I hurt from where my horse slipped over on the slippery ground since it finally rained here in Melbourne. He was fine of course.
The next book has far too many XML chapters in it, or at least it seems that way at the moment. I had a good anti-WS-bloat rant in Amsterdam recently. In a nutshell: most of the web services stack is bloatware that doesn’t get used for most applications. 90% of the time you can get away with knowing no SOAP, WSDL, or any of that stuff. You can just use REST – Representational State Transfer. This is basically an HTTP GET request with parameters, and in return you get an XML document, in other words XML over HTTP. It works very nicely and is simple. I was trying to explain this to somebody here at work, so I wrote a very basic REST server/client pair in PHP in five minutes to demonstrate. Here’s the code, which is doubled in size due to comments.
REST server for imaginary stock price lookup
// check which stock we want to look up
$stock = $_GET[‘stock’];
// look up the stock
$price = lookup($stock);
// format stock quote as XML
$string = "<stockquote><stockprice>$price</stockprice></stockquote>";
// make it into a proper XML document with header etc
$xml = simplexml_load_string($string);
// send an XML mime header
// output correctly formatted XML
// that’s the end of the main code, function below is just a stub
// all stocks are worth $1 for this basic example
// but you would look it up in a database if you had one
REST client ditto
// query the REST server and load the returned XML as a PHP object
// note parameters go to REST server as HTTP GET parameters
$xml = simplexml_load_file("http://localhost/rest/restserver.php?stock=$stock");
echo "$stock stock price today is ";
Basically you don’t need to know anything about Web Services or even XML to make and consume REST Web Services. This assertion greatly irritates a lot of people.
I’m not saying there isn’t a need for SOAP and WS-*, just that often simple tools will do the job. It’s just another version of the 80% rule. This seems to be a theme in my personal consumption of technology.
For further discussion on this subject by others who rant more eloquently, you can read Tim Bray’s blog.