James Devine

Output AF Element / Attribute Recorded Data Using PI Web API in PHP

Blog Post created by James Devine on Nov 11, 2016

The following simple PHP coded page connects to the PI Web API and then outputs the time stamps and recorded values for your target AF Element / Attribute to a webpage:

 

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


<?php
  //~ List of AF Databases:
  $url_root = "https://{{YOUR_DEVICE_NAME}}/piwebapi/";
  $prefix = "streams";
  $webId = "{{YOUR_AF_ELEMENT|ATTRIBUTE_WEBID}}";
  $extension = "recorded";  //~ optional: add start time and end time here
  $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

  echo "<h2>Your Choice For A Heading</h2>";
  $length = count($obj->Items);
  for ($i=0; $i < $length; $i++){
  echo "<p>Timestamp: <span>" . $obj->Items[$i]->Timestamp . "</span>";
  echo " ~ ";
  echo "Value: <span>" . $obj->Items[$i]->Value . "</span></p>";
  }
?>



 

</body>
</html>

Outcomes