A REST service for Meaningful Healthcare

by Wesley

Meaningful Healthcare is a Birmingham-based medical workflow planning company run by Mike Moseley.  Mike recently asked me to build and tie into a series of REST services with his workflow application (built using Gliffy’s API), which lives on Google Sites.  I had a good experience setting up and reviewing the applicable frameworks.

While I’m still a big fan of the SOAP protocol, REST is certainly a bit easier to consume for small-scale frameworks and languages (anything not .NET). I’m a bit disappointed with the recent shift away from SOAP by numerous data providers, but REST is easy enough to consume and write, even if the API implementations are a bit sloppy.  While I was setting this application up, I decided to use PHP since the examples for the third party I would be interfacing with were all written in it, and I could speed things up by using their example code as a stepping stone.

If you plan to write a full end-to-end application in PHP, to include a REST service, I’d recommend using the Zend Framework.  That buys you an MVC setup, and REST formatting/routing without much effort on your part.  However, if you just need a quick and dirty service, have a look at this one page php REST solution (in depth explanation here).

I was impressed with its simplicity, and was able to configure my service fairly quickly.  While this solution may not cover every base, it makes data formatting, routing and deployment as easy as any other PHP app.

Your base REST code ends up looking something like this:

<?php 
include "RestServer.php"; spl_autoload_register(); // don't load our classes unless we use them $mode = 'production'; // 'debug' or 'production' $server = new RestServer($mode); // $server->refreshCache(); // uncomment momentarily to clear the cache if classes change in production mode $server->addClass('YourController', '/directory/subdirectory/YourRESTApi'); $server->handle(); ?>

And routing becomes as easy as this:

/**
* Returns a JSON string object to the browser when hitting the root of the domain
*
* @url GET /
* @url GET /your_directory/$folder_name
*/
public function test($folder_name = null) {
   return ""Hi there.  This is my REST Service: "" .$folder_name;
}