Tuesday, May 4, 2010

WebTV API

Hello,

for those of you who would like to do a little more with your website and your webtv, I have added an API to the webtv so you can control it with javascript from within your own website. The API offers 3 functions to you:

toggleChannels()
togglePlayer()
playChannel('xxxxx')

As you can imagine toggleChannels() will open the channel list but it will also refresh the channel list. togglePlayer() will open the player, displaying the last channel that was playing. playChannel('xxxxx') will play the channel number xxxxx. You can find the number for each of your channels in your channel list xml file. A link to this file is given in your control panel here.

I have posted a demonstration of how you can use the API here. Here is the code I have used to make links to control the webtv:

<a href="javascript:void(togglePlayer())" target="_self">Player</a>
<br />
<a href="javascript:void(toggleChannels())" target="_self">Channels</a>
<br />
<a href="javascript:void(playChannel('58777'))" target="_self">JustinTV Demo</a>
<br />
<a href="javascript:void(playChannel('58778'))" target="_self">Ustream Demo</a>
<br />
<a href="javascript:void(playChannel('58785'))" target="_self">Youtube Demo</a>

If you wish, you could also use buttons or images instead of links to control the webtv. The API should give you all the freedom you need in designing the webtv so it really has the look you want.

You may find it tedious to go manually updating your channel list on your website. To overcome this obstacle you can use php with simplexml and parse your webtv xml file. You can find a demonstration of this technique here. Here is the php code to create the channel list dynamically:

<!-- Start of the custom channel list -->
<div style="float:left; width:200px; height:300px; overflow:auto;">
<a href="javascript:void(togglePlayer())" target="_self">Player</a>
<br />
<a href="javascript:void(toggleChannels())" target="_self">Channels</a>
<br />
<?php
$webtv = "BlackTV";

// Get your webtv channels from mywebtv server
$channels = simplexml_load_file("http://mywebtv.cc/$webtv.xml");
// If your webserver doesn't support allow_url_fopen (see phpinfo())
// then replace the above line with the following
// Copy you channels file in you webserver directory
//$channels = simplexml_load_string(file_get_contents("$webtv.xml"));

// You can make a button for each channel that is online
foreach ($channels->channel as $channel)
if ($channel->online)
echo "<a href=\"javascript:void(playChannel('$channel->id'))\" target=\"_self\" title=\"$channel->description\">$channel->name</a><br />";
?>
</div>
<!-- End of the custom channel list -->

In order to load your channel list xml file into a simplexml object you will need to do one of two things. Either you fetch it directly on mywebtv server, in which case you are sure it is fresh. This is the better option. If however your php server doesn't allow you to fetch a file on a remote server, which is the case with most free php webservers, then you will need to copy your xml file on your server. I have added a FTP Server option in your control panel (here) if you wish MyWebTV to upload your xml file on your ftp server to keep the file fresh.

It may be of some interest to you to know I have found 2 free php servers supporting remote files access and simplexml with php:
- http://www.freewebhostingarea.com/
- http://www.000webhost.com/

Finally, it's important to note you will need to update your webtv code with the latest embed code available from your control panel in order to use the webtv api.

Enjoy,
Elvis

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home