Kristian Lunde

www.klunde.net

Archive for the ‘JSON’ tag

Writing SOA applications with PHP

without comments

Lately I’ve been working a lot with data integration between several web applications, and a natural choice for the integration was to use a Service Oriented Architecture (SOA). I’ve built both SOA servers and clients before using the SOAP approach, which is a superb way of transferring data when not knowing who the user of the service is, or when you are a client of such a service. However in this setting I knew who the user was, I knew what the service would be used for, also had the chance to write both the client and server.

The choice fell on a simple implementation using the JSON (JavaScript Object Notation) data structure. This is really easy using the built in json library in php. The server use the json_encode() function and the implemented sub class of the client use the json_decode() function.

To simplify this further I implemented two abstract classes, a ServiceServer class and a ServiceClient class. The service server class contained a very simple displayJSONResult function, which sets the correct header, encode the data as json data and echo the data.

The service client class has two main functions, doGetRequest and doPostRequest, both functions use the cURL library in PHP.

Server

  1. <?php
  2.  
  3. abstract class ServiceServer
  4. {
  5.  
  6.  public function __construct(){}
  7.  
  8.  protected function displayJSONResult($data)
  9.  {
  10.   header('Content-type: text/plain');
  11.  
  12.   echo json_encode($data);
  13.  
  14.   exit();
  15.  }
  16. }

Client

  1. <?php
  2.  
  3. /**
  4.  * ServiceClient
  5.  *
  6.  *
  7.  * @author Kristian Lunde
  8.  *
  9.  */
  10.  
  11. class ServiceClient
  12. {
  13.  
  14.  public function __construct(){}
  15.  
  16.  /**
  17.   * do a post request to a service
  18.   *
  19.   * the params parameter must be a string with the format:
  20.   * key=val&key2=val2&key3=val3
  21.   *
  22.   * @param string $url
  23.   * @param string $params
  24.   */
  25.  protected function doPostRequest($url, $params)
  26.  {
  27.   $ch = curl_init($url);  
  28.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
  29.   curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
  30.   curl_setopt($ch, CURLOPT_POST   , 1);
  31.    curl_setopt($ch, CURLOPT_POSTFIELDS     , $params);
  32.   $result = curl_exec($ch);
  33.   curl_close($ch);
  34.   return $result;
  35.  }
  36.  
  37.  /**
  38.   * do a get request to a service
  39.   *
  40.   * @param string $url
  41.   *
  42.   * @return mixed
  43.   */
  44.  protected function doGetRequest($url)
  45.  {
  46.   $ch = curl_init($url);  
  47.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
  48.   curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
  49.   $result = curl_exec($ch);
  50.   curl_close($ch);
  51.   return $result;
  52.  }
  53.  
  54. }

Example

To illustrate the easiness and simplicity of this SOA approach I written a small example. In this example the server finds the country of a city using POST parameters or it can find cities using the country as a GET parameter.

Example server

  1. <?php
  2.  
  3. /**
  4.  * Example of using the ServiceServer class
  5.  *
  6.  * Returns countries or cities
  7.  *
  8.  * @author Kristian Lunde
  9.  */
  10.  
  11. require_once('ServiceServer.php');
  12.  
  13. class ExampleServer extends ServiceServer
  14. {
  15.  
  16.  
  17.  
  18.  public function __construct()
  19.  {
  20.   $this->countries = array('norway'  => array('Oslo',
  21.               'Trondheim',
  22.               'Bergen',
  23.               'Halden',
  24.               'Sarpsborg',
  25.               'Hammerfest'),
  26.          'sweden' => array('Stockholm',
  27.               'Gothenburg',
  28.               'Karlstad'),
  29.          'england' => array('London',
  30.               'Newcastle',
  31.               'Bath',
  32.               'Liverpool'));  
  33.  
  34.  
  35.   if(isset($_GET['country']))
  36.   {
  37.    $this->findCitiesByCountry(trim($_GET['country']));
  38.   }
  39.  
  40.   if(isset($_POST['city']))
  41.   {
  42.    $this->findCountryByCity(trim($_GET['city']));
  43.   }
  44.  }
  45.  
  46.  public function findCitiesByCountry($country)
  47.  {
  48.   $cities = 'Not found';
  49.   $country = strtolower($country);
  50.  
  51.   if(isset($this->countries[$country]))
  52.   {
  53.    $cities = $this->countries[$country];
  54.   }
  55.  
  56.   $this->displayJSONResult($cities);
  57.  }
  58.  
  59.  public function findCountryByCity($city)
  60.  {
  61.   $country = 'Not found';
  62.  
  63.   $break = false;
  64.   foreach($this->countries as $key => $val)
  65.   {
  66.    for($i = 0, $count = count($val); $i < $count; $i++)
  67.    {
  68.     if($city == $val[$i])
  69.     {
  70.      $country = $key;
  71.      $break = true;
  72.      break;
  73.     }
  74.    }
  75.    
  76.    if($break)
  77.    {
  78.     break;
  79.    }
  80.   }
  81.  
  82.   $this->displayJSONResult($country);
  83.  }
  84. }
  85.  
  86. $obj = new ExampleServer();
  87. ?>

Example client

  1. <?php
  2. /**
  3.  * Example of using the ServiceClient class
  4.  *
  5.  * does a request to the ExampleServer
  6.  *
  7.  * @author Kristian Lunde
  8.  */
  9.  
  10. require_once('ServiceClient.php');
  11.  
  12. class ExampleClient extends ServiceClient
  13. {
  14.  public function __construct()
  15.  {
  16.   $this->getCities('Norway');
  17.   $this->getCounty('Bath');
  18.  }
  19.  
  20.  public function getCities($country)
  21.  {
  22.   $url = 'http://files.klunde.net/files.klunde.net/ExampleServer.php?country=' . $country;
  23.   echo $this->doGetRequest($url);
  24.  }
  25.  
  26.  public function getCountry($city)
  27.  {
  28.   $url = 'http://files.klunde.net/files.klunde.net/ExampleServer.php';
  29.   $params = 'city=' . $city;
  30.  
  31.   echo $this->doPostRequest($url, $params);
  32.  }
  33. }
  34. $obj = new ExampleClient();
  35. ?>

I’m quite satisfied with this implementation because it is so easy to maintain and even more important, implementing new services and clients for the services is straight forward and supports rapid development.

Download

ServiceServer.phps

ServiceClient.phps

ExampleServer.phps

ExampleClient.phps

Written by Kristian Lunde

June 8th, 2008 at 10:28 pm

Get Adobe Flash player