Simple API Tutorial
From Jtvcommunity
This is a small guide on how to get live status data from Justin.tv and Twitch.tv. I will be demonstrating this in PHP and I hope that you can easily modify this for whatever language your website is using.
Contents |
Step 1) Build a list of streams you are interested in.
In your code, you will need to build a comma separated list of all the streams you are interested in.
So if you have a database query called from a table named “channels” and a value called “stream_name” on that channel, you could do the following
foreach ($channels as $c)
{
$stream_list = $stream_list . $c->stream_name . ',' ;
}
Alternatively, if you are doing this directly simply make the string in place.
$stream_list = "mystream1,mystream2,mystream3";
Step 2) Request the status from the justin.tv website.
Make a web request to
http://api.justin.tv/api/stream/list.json?channel=$stream_list
or
http://api.justin.tv/api/stream/list.xml?channel=$stream_list
Depending on if you want JSON or XML to be returned to you. In this example I will use CURL to make the web resquest and JSON as the returned datatype.
//Create an instance of curl, ignore the headers, return into variables $mycurl = curl_init(); curl_setopt ($mycurl, CURLOPT_HEADER, 0); curl_setopt ($mycurl, CURLOPT_RETURNTRANSFER, 1); //Build the URL $url = "http://api.justin.tv/api/stream/list.json?channel=" . $stream_list; curl_setopt ($mycurl, CURLOPT_URL, $url); $web_response = curl_exec($mycurl);
Step 3) Parse your results.
Now that you have a results variable, you need to parse it somehow. This example uses JSON decoding but if you use XML you will have to decode it in an alternative manner.
$results = json_decode($web_response);
Step 4) Use your results.
Now you can use the $results variable to programatically alter your site.
There are two cases to consider, one of the cases is when all no channel is online. In that case results will be exactly “[]” and json_decoding will return an empty array. The other case is when one or more channels are live. There are several interesting values that you should check out by making a live request in your browser and looking at the results. Here we will use channel_count and the screen capture from the screen to display some information to standard output.
foreach($results as $s) {
echo $s->channel->login . " is live\n";
echo $s->title . "\n";
echo $s->channel_count . " viewers watching\n";
echo $s->channel->screen_cap_url_large . " is the url for the screen capture\n";
}
Full File
Here is the full source code. Paste it into a text file named test.php and run it on your webserver, or include it in a php file, or run it from the command line to see example results. You have to change the $stream_list variable to a comma separated lists of channels you are interested in.
<?php
$stream_list = "mystream1,mystream2,mystream3";
$mycurl = curl_init();
curl_setopt ($mycurl, CURLOPT_HEADER, 0);
curl_setopt ($mycurl, CURLOPT_RETURNTRANSFER, 1);
//Build the URL
$url = "http://api.justin.tv/api/stream/list.json?channel=" . $stream_list;
curl_setopt ($mycurl, CURLOPT_URL, $url);
$web_response = curl_exec($mycurl);
$results = json_decode($web_response);
foreach($results as $s) {
echo $s->channel->login . " is live\n";
echo $s->title . "\n";
echo $s->channel_count . " viewers watching\n";
echo $s->channel->screen_cap_url_large . " is the url for the screen capture\n";
}
?>
As a final note, if you would like to directly show the video player directly on your page, you can do the following to put an embed for every stream into your page.
foreach($results as $s) {
echo $s->channel->embed_code;
}
