I just started working with json for real and trying to learn as best as I can! I want to share this work I made, I feel like this could need some improvements, if not just much of it.
So okay, I use twitch.tv REST_API. And here is my code. Basically I want to run this every minute as a crontab through my web hosting company. I know that you can get the (encoded) json data through this way: "http://api.justin.tv/api/stream/list.json?channel=example,example2,example3"; as well. And it is probably faster? but then I don't know how to set my stream offline in the database.
So I guess what I am asking is just how I can improve this.
$result = mysql_query("SELECT streamname FROM streams") or die(mysql_error());
$ids=array();
while($row = mysql_fetch_assoc($result))
{
$ids[]=$row["streamname"];
}
$mycurl = curl_init();
for($i=0;$i<count($ids);$i++)
{
curl_setopt ($mycurl, CURLOPT_HEADER, 0);
curl_setopt ($mycurl, CURLOPT_RETURNTRANSFER, 1);
$url = "http://api.justin.tv/api/stream/list.json?channel=$ids[$i]";
curl_setopt ($mycurl, CURLOPT_URL, $url);
$web_response = curl_exec($mycurl);
$result = json_decode($web_response);
if(empty($result))
{
$sql = "UPDATE streams SET online = '0' WHERE streamname = '" . $ids[$i] . "'";
}
else
{
$sql = "UPDATE streams SET online = '1' WHERE streamname = '" . $ids[$i] . "'";
}
mysql_query($sql) or die(mysql_error());
}