Example - Web service and PHP scripts

Below is an example of web service in PHP. It allows to listen to POST requests in JSON format and save results to disk in a directory (here alerts_json/alert)

<?php
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', substr(@$_SERVER['PATH_INFO'], 1));

switch ($method) {
case 'POST':
  $jsonAlert = file_get_contents('php://input');
  $alert = json_decode($jsonAlert, true);
  $fileName = 'alerts_json/alert_' . $alert['occurence.key'];
  file_put_contents($fileName, $jsonAlert, FILE_APPEND | LOCK_EX);
  break;
default:
  break;
}
?>

Then the following script in PHP (installed in the web service directory) allows to read alerts saved to disk and display them in a table:

<?php
header('Content-Type: application/json');

if ($handle = opendir('alerts_json')) {
  $entries = array();
  while (false !== ($entry = readdir($handle))) {
    if (substr($entry, 0, 5) === "alert") {
      $fileName = "alerts_json/" . $entry;
      array_push($entries, json_decode(file_get_contents($fileName), true));
      unlink($fileName);
    }
  }
  echo json_encode($entries);
  closedir($handle);
}
?>