xml, web services, simple things are beautiful, yada yada yada

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
  header("Content-type: text/xml");

  // output correctly formatted XML
  echo $xml->asXML();

// that’s the end of the main code, function below is just a stub

  function lookup($s)
    // all stocks are worth $1 for this basic example
    // but you would look it up in a database if you had one
    return 1;

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 ";
echo $xml->stockprice;


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.

6 thoughts on “xml, web services, simple things are beautiful, yada yada yada”

  1. I am confident you know well enough XML-RPC (and its implementations in PHP) to have pondered over its benefits/disadvantages vis-a-vis REST, but for me it sits really in the middle of the 80/20 magical line: very easy to understand, easy enough to implement, plenty of libs for every possible platform under the sun

Leave a Reply

Your email address will not be published. Required fields are marked *