REST API result in HTML element

Hey guys,

so, I’ve got an OpenHAB server running that connects to my Sonos Play.
It works pretty well and is also available via my.openhab.

The sound is pretty amazing, unfortunately my neighbors (all students sigh) do not like me partying late at night.
Until now they always used to complain via Whatsapp, until I setup a simple HTML page, where they can turn my music volume down to 50% remotely.

That’s pretty cool, so I’ve thought about publishing the currently playing track, album and artist on the page. (That way they know what beautiful melody interrupted their sleep)

So, I’m a total beginner in web development, that’s why I’m asking:

How do I display the result of a specific openhab REST call on a html page?

I’ve got the following PHP:

<?php
function getStatus($item) 
{
      $url = "https://my.openhab.org:443/rest/items/" . $item;

      $options = array(
        'http' => array(
            'header'  => "Content-type: text/plain\r\n",
            'method'  => 'GET',
        ),
      );

      $context  = stream_context_create($options);
      $result = file_get_contents($url, false, $context);

      return $result;
}
getStatus("Title");
?>

When I run this request in postman, I’m getting a proper result:

{
  "link": "https://my.openhab.org/rest/items/Title",
  "state": "Giles",
  "type": "String",
  "name": "Title",
  "tags": [],
  "groupNames": []
}

So, question is how do I get this “state” result into - say a HTML Tag?

I know this might be trivial, but I’m such a noob when it comes to php and html

Hey there,

Did you get this solved? I have the exact same requirement, maybe you could post the code you finally ended up with here?

Thx!

Not yet. But I also haven’t had the time to dig deeper into this. If I get it solved, I will post an update here.

Actually, in the meantime I solved this. Just a few words on what my exact problem was:

I wanted to display the weather on an old Amazon Kindle - unfortunately the browser in the kindle wasn’t able to display HABPanel, that’s why I looked for a solution which would display things in a way that could be displayed by the kindle’s browser.

What I came up with is similar to your idea: use php to retrieve data from the REST API and then display this results in html.

Here’s the php-code (I used curl):

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://openhab:8080/rest/items/temperature_outside/state');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);
?>

And then you just put this into where you want the result to be displayed in the html:

<?php echo $response; ?>

Please note that I only created one php-file which contains the html-code. My php knowledge is really basic, so I can’t get a separate php and html file to run.

Hope that helps

Your example helped me accomplish what i wanted to do. To get the state make your code look like this:

<?php
function getStatus($item) 
{
      $url = "http://192.168.2.3:8080/rest/items/" . $item;

      $options = array(
        'http' => array(
            'header'  => "Content-type: text/plain\r\n",
            'method'  => 'GET',
        ),
      );
      $context  = stream_context_create($options);
      $json = file_get_contents($url, false, $context);
      $state = json_decode($json);
      $result = $state->{'state'};
      return  $result;
}
?>
<?php

$myItemName = getStatus("myItemName");
echo $myItemName;
?>

This would output “Giles” :slight_smile: