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
<?php

  // 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
$stock=’MSFT’;

$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 Comments

  1. Max Case:

    Hi

    A link to REST would be handy

    thanks

  2. Laura:

    I’m told the term REST was coined by Roy Fielding in his PhD thesis. You can read the relevant chapter here:
    http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

  3. Douglas Clifton:

    Paul James is a REST advocate and he maintains a blog you might want to visit:

    http://www.peej.co.uk/thinking/rest

    There is also a REST mailing list for developers:

    http://groups.yahoo.com/group/rest-discuss

    Hi Laura, enjoyed your book. Drop me an email if you get the chance.

    Doug

  4. Gaetano Giunta:

    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

  5. Laura:

    Yep, I also know and like XML-RPC - deliberately kept simple and elegant.

  6. tanmay:

    Your style rocks!! I would read more about SOAP and W-*. But you gave me the best solution.

Leave a comment