you can generate a JWT via API as well… and with this JWT you can use the rest of the api-functions.
here is a function to generate a JWT
function getJWT() {
$url = "http://your-chipstark-app-server:8080/api/internal/login";
$data = array(
'password' => 'YourPassword',
'username' => 'YourUsername',
);
$payload = json_encode($data);
$ch = curl_init($url);
$request_headers = array();
$request_headers[] = 'Content-Type: application/json';
$request_headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$answer = curl_exec($ch);
$data = json_decode($answer);
return $data->jwt;
curl_close($ch);
}
this generates a JWT which can be used for the rest of the commands - for example if you want to get all your gateways:
$jwt = getJWT();
$orgid = getOrgID($id);
$gwlimit = getGWLimit($server, $orgid, $jwt);
// jSON URL which should be requested
$url = $server . "gateways?limit=" . $gwlimit . "&organizationID=" . $orgid;
$ch = curl_init($url);
$request_headers = array();
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Grpc-Metadata-Authorization: Bearer '.$jwt.'';
// Setting curl options
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Getting results
//$result = curl_exec($ch); // Getting jSON result string
$answer = curl_exec ($ch) ; // Getting jSON result string
$data = json_decode($answer);
now you have a array ($data) with json_decoded values (the gateways)
then you have to loop the array to get all the data:
if (count($data->result)) {
// echo “<table id="test123" class="uk-table">”;
echo “<table id="test123" class="uk-table table table-striped table-bordered">”;
echo "<thead>";
echo "<tr>";
echo "<th>Gateway-Name</th>";
echo "<th>Gateway-ID</th>";
echo "<th>Description</th>";
echo "<th>Details</th>";
echo "</tr>";
echo "</thead>";
// Cycle through the array
echo "<tbody>";
foreach ($data->result as $idx => $result) {
// Output a row
echo "<tr>";
echo "<td>$result->name</td>";
echo "<td>$result->id</td>";
echo "<td>$result->description</td>";
echo "<td> <a href=\"index.php/iot/gwdetails?gwid=" . $result->id . "\"> Details </a> </td>";
echo "</tr>";
}
echo "</tbody>";
// Close the table
echo "</table>";
}
you can see in the part of this part - building the html table in php. second part (// Cycle through the array) you loop the array to get all the data.
i hope it makes the idea clear. of course, if you need something else then gateways, your neet to change the url.