James Devine

List of AF Databases Using PI Web API in PHP

Blog Post created by James Devine on Sep 19, 2016

The following simple PHP coded page connects to the PI Web API and then outputs the list of AF Databases for your target AF Server to a webpage:

<!DOCTYPE html>
<title>List AF Databases</title>
     body { margin-left: 5%; margin-right: 5%; }
     span {font-family: Courier New; }

  //~ List of AF Databases:
  $url_root = "https://{{YOUR_DEVICE_NAME}}/piwebapi/";
  $prefix = "assetservers";
  $webId = "{{YOUR_AF_SERVER_WEBID}}";
  $extension = "assetdatabases";
  $json = file_get_contents($url_root . $prefix . "/" . $webId . "/" . $extension); //~ Get the JSON data from PI Web API
  $obj = json_decode($json); //~ decode the JSON data for use in PHP

  $length = count($obj->Items);
  for ($i=0; $i < $length; $i++){
  echo "<h2>";
  echo "Database Name: <span>" . $obj->Items[$i]->Name . "</span>";
  echo "</h2>";
  echo "<p>";
  echo "WebId: <span>" . $obj->Items[$i]->WebId . "</span><br>";
  echo "Path: <span>" . $obj->Items[$i]->Path . "</span><br>";
  echo "</p>";
  echo "<hr>";