关于将两个XML feed组合在一起的建议 - own3d / twitch.tv

时间:2012-06-30 16:30:21

标签: php xml api

我正在开展一个小项目来展示Dota 2,英雄联盟和星际争霸2的实时流,并且正在使用第三方托管的api,它既有来自Twitch.tv又拥有自己的3D - 我不能更高兴,直到他们关闭他们的服务器!

我尝试将它们与雅虎管道合并,但没有太多运气! 这些是我正在使用的两个Feed:

Twitch.tv/justin.tv - http://api.justin.tv/api/stream/list.xml?category=gaming&limit=15

Own3d - http://api.own3d.tv/live?limit=15

以下是我尝试将两个Feed组合在一起,只是将它们列在彼此之上。它是一个临时解决方案,但它们不是由现场观众排序(这是我想做的事情) -

我目前的剧本:

<table class="table">
<thead>
<tr>
<th colspan="4" class="streamheader">Current live Sstreams</th>
</tr>
</thead>

<tbody>

<?php

$xmlFileData2 = file_get_contents("http://api.own3d.tv/live?limit=15");

$xmlData2 = new SimpleXMLElement($xmlFileData2);

foreach($xmlData2->channel->item as $item) 

if ($item->misc['game'] == 'Dota 2' OR $item->misc['game'] == 'League of Legends' OR $item->misc['game'] == 'StarCraft II') {

{

$titlelimit = $item->title;

$title = substr($titlelimit,0,20);

echo "<tr><td>";

if ($item->misc['game'] == 'League of Legends')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/lol.jpg' /></td><td>";
elseif ($item->misc['game'] == 'StarCraft II')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/sc2.jpg' /></td><td>";
elseif ($item->misc['game'] == 'Dota 2')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/dota2.jpg' /></td><td>";

else
echo "none";

echo "<a href='";
$link = $item->link;             
$findthis ="/www.own3d.tv/live/"; 
$replacement ="ae/stream/"; 
echo str_replace ($findthis, $replacement, $link);
echo "'>";
echo $title;
echo "</a></td><td>";
$author = $item->author;             
$find ="/rss@own3d.tv/"; 
$replace =""; 
echo preg_replace ($find, $replace, $author);
echo "<td>";
echo $item->misc['viewers']; 
echo "</td> </tr>";

}
}
else
{
echo "";
}

$xmlFileData1 = file_get_contents("http://api.justin.tv/api/stream/list.xml?category=gaming&limit=15");

$xmlData1 = new SimpleXMLElement($xmlFileData1);

foreach($xmlData1->stream as $itemtwitch) {

$game = $itemtwitch->meta_game;

$sc2 = "StarCraft II: Wings of Liberty";

if ($itemtwitch->meta_game == 'Dota 2' OR $itemtwitch->meta_game == 'League of Legends' OR $itemtwitch->meta_game == 'StarCraft II: Wings of Liberty') {
{
echo "<tr><td>";

$titlelimittwitch = $itemtwitch->title;

$titlelimittwitch = substr($titlelimittwitch,0,20);

if ($itemtwitch->meta_game == 'League of Legends')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/lol.jpg' /></td><td>";
elseif ($itemtwitch->meta_game == 'StarCraft II: Wings of Liberty')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/sc2.jpg' /></td><td>";
elseif ($itemtwitch->meta_game == 'Dota 2')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/dota2.jpg' /></td><td>";
else
echo "none";

$data = $itemtwitch->name;
$find ="/live_user_/";
$replace ="";
echo "<a href='";
echo "http://localhost/ae/stream/";
echo preg_replace ($find, $replace, $data);
echo "''>";
echo "$titlelimittwitch";
//print(" - " . $itemtwitch->meta_game . "");
echo "</a></td><td>";
echo preg_replace ($find, $replace, $data);
print("</td><td>" . $itemtwitch->channel_count . "</td></tr>");
}
}
else {
echo "";
}
}
?>

<tr>

<th colspan="4" class="streamheader centered">View all streams</th>
</tr>   
</tbody>
</table>

任何指导或指向正确的方向都会很棒。

干杯!

2 个答案:

答案 0 :(得分:0)

您可以使用像Streamtastic这样的库(免责声明:我写了)来获取Own3d和Twitch.tv的最新和最常查看的流

https://github.com/sergiotapia/Streamtastic

用法非常简单:

Own3d示例:

StreamFinder own3dStreamtastic = new StreamFinder(new Own3dStreamRepository());

// Let's find the most viewed streamers in general.
var topStreamers = own3dStreamtastic.FindTopViewedStreams();
foreach (var stream in topStreamers)
{
    Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
}

// You can find the top viewed streamers for any game Own3d supports.
// For example, League of Legends.
var topStreamersForLeagueOfLegends = own3dStreamtastic.FindTopViewedStreamsByGame("League+of+Legends");
foreach (var stream in topStreamersForLeagueOfLegends)
{
    Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
}

// Or how about League of Legends.
var topStreamersForLol = own3dStreamtastic.FindTopViewedStreamsByGame("League+of+Legends");
foreach (var stream in topStreamersForLol)
{
    Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
}

由于您使用的是PHP,只需运行此应用程序并将结果保存到数据库即可。然后让你的PHP程序只从数据库中获取结果。

答案 1 :(得分:0)

Dan,您需要将两组信息(own3d / twitch)添加到同一个数组中,然后对其进行排序。假设您将它们添加到名为$ streamArray的数组中,您将使用以下代码然后由查看者对它们进行排序。

foreach ($streamArray as $key => $row) {
 $viewers[$key] = $row['viewers'];
}

array_multisort($viewers, SORT_DESC, SORT_NUMERIC, $streamArray);

我在http://halfw.com/streams.php

开发了类似的设置

如果您有任何疑问,请告诉我们!