I am trying to fetch information out of SHOUTcast for mutiple users at the same time. Here is my current code:
<?php
header("Access-Control-Allow-Origin:*");
function failOut($http, $text) {
header('HTTP/1.x ' . $http);
die(htmlspecialchars($text));
}
$params = $_GET;
$user = 'root'; // Change this to username
$password = ''; // Put your password here. My home database doesn't have any password (is empty).
$con = mysql_connect('localhost',$user,$password) or die('Could Not Connect..');
$context = stream_context_create(
array(
'http' => array(
//Required To fool ShoutCast or it will flood with the stream instead
'user_agent' => "GET / HTTP/1.0\r\n"
."Host:117.200.149.48\r\n"
."User-Agent: Mozilla/5.0 (compatible; ShoutCastInfoClass/0.0.2; ".PHP_OS.")\r\n"
."\r\n",
),
)
);
if($params['act'] == 'st'){
mysql_select_db('radio',$con);
$count = mysql_query("select * from info where ID='".$params['id']."'")or die('Error In Query'.mysql_error());
$content = null;
$rows = mysql_num_rows($count);
if( $rows > 0 ){
$res = mysql_fetch_assoc($count);
if($res['TIMESTAMP'] + 30 < time()){
if($res['version'] == '1'){
$base = $res['IP'].'/admin.cgi?mode=viewxml&user=admin&pass='.$res['PASS'];
// Well i will change this crazy method with something non password secured pretty soon
}else{
$base = $res['IP'].'/stats?sid=1';
// As per shoutcast v2 APIs !! :D
}
$content = file_get_contents($base, false, $context)or die("Unable to Load resourse");
$dom = new DOMDocument();
$dom->loadXML($content);
$res['SERVERTITLE'] = htmlspecialchars($dom->getElementsByTagName('SERVERTITLE')->item(0)->textContent);
$res['SONGTITLE'] = htmlspecialchars( $dom->getElementsByTagName('SONGTITLE')->item(0)->textContent );
$res['BITRATE'] = $dom->getElementsByTagName('BITRATE')->item(0)->textContent;
$res['GENRE'] = $dom->getElementsByTagName('SERVERGENRE')->item(0)->textContent;
$res['CONTENT'] = $dom->getElementsByTagName('CONTENT')->item(0)->textContent;
if(strpos($res['CONTENT'],'mpeg')){
$res['CONTENT'] = 'MP3';
}else if(strpos($res['CONTENT'],'aac')){
$res['CONTENT']= 'AAC';
}else{
$res['CONTENT'] = 'OGG';
}
$query='update INFO SET SERVERTITLE="'.$res['SERVERTITLE'].
'", SONGTITLE="'.$res['SONGTITLE'].
'", BITRATE="'.$res['BITRATE'].
'", GENRE="'.$res['GENRE'].
'", CONTENT="'.$res['CONTENT'].
'" , TIMESTAMP='.time().' where ID=\''.$params['id']. '\';';
mysql_query($query,$con)or die(mysql_error());
mysql_close($con);
}
unset( $res['IP'] );
unset( $res['PASS'] );
echo json_encode($res);
}
}else if($_params['act']== 's'){
// To search for a specific keyword
$queryString = $params['q'];
}else if($_params['act'] == 'l'){
// To feed list to the applet
$cat = $params['c'];
// Categories are various lists depending on genre languages etc...
}
?>
I was wondering if making a phpSocket server would be better for this purpose as sockets will reduce the demand of required MySQL connections to be opened.
Or
Is my current method the best solution?