人们开始抱怨我的网站速度很慢。我需要你的帮助来确定问题。在找到更好的解决方案后,我一直在疯狂地搜索互联网,但没有成功。我正在尝试从 justin.tv/twitch 获取流名称以及流媒体有多少访客。
目前我正在使用他们自己的 API wiki 页面中的入门代码。然而,它非常缓慢。我有 52 个流存储在我的 mySQL 数据库中,我将它们放入一个数组中,然后使用 json 来解析数据。
<?php
$result = mysql_query("SELECT streamname FROM streams") or die(mysql_error());
$ids=array();
while($row = mysql_fetch_assoc($result))
{
$ids[]=$row["streamname"];
}
$stream_list = implode(",", $ids);
$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 "<a href='stream.php?watch=" . $s->channel->login . "'>" . $s->channel->login . " " . $s->channel_count . " viewers</a><br />";
}
?>
更新。是的,我使用我的 MySQL 来设置每个频道的类别类型。我也试过没有这个,速度差异不是很大。所以它仍然是需要时间加载的json。这是我如何使用 MySQL
$result = mysql_query("SELECT streamname FROM streams WHERE race = 'terran' OR race = 'protoss' OR race = 'zerg'") or die(mysql_error());
$ids=array();
while($row = mysql_fetch_assoc($result))
{
$ids[]=$row["streamname"];
}
$stream_list = implode(",", $ids);
$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);
echo "<div id=\"tab1\">";
foreach($results as $s)
{
// get race
$sql = mysql_query("SELECT race, streamname, name FROM streams WHERE streamname = '" . $s->channel->login . "'") or die(mysql_error());
$row = mysql_fetch_array($sql, MYSQL_BOTH);
$race = $row['race']; // race
$streamername = $row['name'];
echo "race: " . $race . " <a href='stream.php?watch=" . $s->channel->login . "'>" . $row['name'] . " " . $s->channel_count . " viewers</a><br />";
}
echo "</div>";