James Devine

List of AF Servers Using PI Web API in PHP

Blog Post created by James Devine on Sep 19, 2016

The following simple PHP page is a very basic approach to connecting to the PI Web API and then outputting the list of AF Servers on a web page using PHP code.

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


  //~ List of AF Servers:
  $url_root = "https://{{YOUR_DEVICE_NAME}}/piwebapi/"; 
  $prefix = "";
  $webId = "";
  $extension = "assetservers";
  $json = file_get_contents($url_root . $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 "Server 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>";