Skip to content

Examples

dave-p edited this page Feb 26, 2019 · 16 revisions

curl

Parameters passed to TVH using the GET method must be URI-encoded:

curl 'http://user:pass@localhost:9981/api/epg/events/grid?limit=999&channel=BBC%20ONE'

Alternatively use the POST Method:

curl --data 'limit=999&channel=BBC ONE' 'http://user:pass@localhost:9981/api/epg/events/grid'

To make the output more human-readable, pipe it through json_pp (included in the perl package on many distributions) or jq.

PHP

This simple example lists some details about upcoming timers, sorted in date order. To work through a PHP-enabled web server, the PHP.INI setting "allow_url_fopen" must be ON.

$timers = get_timers();
foreach($timers as $t) {
    $start = strftime("%H:%M", $t["start"]);
    $stop = strftime("%H:%M", $t["stop"]);
    $date = strftime("%a %e/%m", $t["start"]);
    echo "Date: $date, Start: $start, End: $stop, Title: {$t['disp_title']}";
}

function get_timers() {
  $url = "http://admin:admin@localhost:9981/api/dvr/entry/grid_upcoming?sort=start";
  $json = file_get_contents($url);
  $j = json_decode($json, true);
  $ret = &$j["entries"];
  return $ret;
}

For an example of what can be done with the API in PHP see https://github.com/dave-p/TVHadmin.

Javascript

It is a 'feature' of Javascript that a script can only access remote content from the same server as the script was loaded from. Hence to call the TVHeadend API from Javascript, the script must be hosted on the TVHeadend server. To do this, place your script in /usr/share/tvheadend/src/webui/static; it can then be accessed from URL http://user:[email protected]:9981/static/. Note that this is unintended behaviour and may change in the future.

This example carries out the same task as the PHP example above. Your TVHeadend user and password are required in the req.open line.

<!DOCTYPE html>
<html>
<head>
  <title>TVH Javascript test</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <style>
    table, th, td {
      border: 1px solid black;
    }
  </style>
  <script type="text/javascript">
    var url = "http://your.server:9981/api/dvr/entry/grid_upcoming?sort=start";
    var req = new XMLHttpRequest();
    req.onreadystatechange=function() {
      if (req.readyState==4 && req.status==200) {
        var data = req.response;
        var html = "<tr><th>Date</th><th>Start</th><th>End</th><th>Title</th></tr>";
        data.entries.forEach(function(entry) {
          var start = new Date(entry.start * 1000);
          var stop = new Date(entry.stop * 1000);
          html += "<tr><td>" + start.toLocaleDateString() +
                  "</td><td>" + start.toLocaleTimeString() +
                  "</td><td>" + stop.toLocaleTimeString() +
                  "</td><td>" + entry.disp_title + "</td></tr>";
        });
        document.getElementById("result").innerHTML = html;
      }
    };
    req.open('GET', url, true, "user", "pass");
    req.responseType = 'json';
    req.send(null);
  </script>
</head>
<body>
  <table id="result"></table>
</body>
</html>
Clone this wiki locally