USTREAMのAPIを使う

ustreamAPI仕様はこちら:
http://developer.ustream.tv/data_api/docs
まず、developer keyが必要なので、それを取得。
http://developer.ustream.tv/user/register

apiのdocsはすごくわかりにくいし、forumもやっていないので全部手探り。


考えている主な使い道は

  • ライブなら、表示
  • 表示は、動画+social streaming


これをするのが、以下

<?php
$uid=[取得したいchannelのuid. http://ustream.tv/channel/*** の ***部];

$request =  'http://api.ustream.tv';
$format = 'php';   // xml | json | html | php
$args .= 'subject=channel'; // channel | user | video | stream | system
$args .= '&uid='.$uid; // subject=channelの場合は、channelのuid
$args .= '&command=getinfo'; // getinfo getvalueof. getvalueofを使う場合は、$argsにparamsを追記
$args .= '&key='.$devkey;

// cURL
$session = curl_init($request.'/'.$format.'?'.$args);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($session);
curl_close($session);

// format=phpなので
$resultsArray = unserialize($response);
// これがid
$id=$resultsArray['results']['id'];
// これが動画
$embedTag=$resultsArray['results']['embedTag'];
// これがstatus 
$status=$resultsArray['results']['status']; // live | offline
// これがERROR。問題なければ空
$error=$resultsArray['error']; // 

// 表示
if ($error=='')
{
	echo 'status:'.$status;
	if ($status=='offline')
	{
		echo '<table><tr><td>'.$embedTag;
		echo '<iframe width="468" scrolling="no" height="260" frameborder="0" style="border: 0px none transparent;" src="http://www.ustream.tv/socialstream/'.$id.'"></iframe>';
	}
}
?>

ただし、

  • このsocial streamingは、"New Social Stream"で"Request the New Social Stream"をチェックしたものにしか、うまく表示されない。
  • APIの制限で、1developer keyで1日あたり5000回まで。