-3

I came across this php script to pull IceCast stats (listeners,current song) from my streaming server.

It was published here

Use PHP to show Icecast2 statistics

<?php

/*
 * SCRIPT CONFIGURATIONS
*/
$SERVER = 'http://myserver.com:8000'; //URL TO YOUR ICECAST SERVER
$STATS_FILE = '/status.xsl'; //PATH TO STATUS.XSL PAGE YOU CAN SEE IN YOUR BROWSER (LEAVE BLANK UNLESS DIFFERENT)

///////////////////// END OF CONFIGURATION --- DO NOT EDIT BELOW THIS LINE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

//create a new curl resource
$ch = curl_init();

//set url
curl_setopt($ch,CURLOPT_URL,$SERVER.$STATS_FILE);

//return as a string
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

//$output = our stauts.xsl file
$output = curl_exec($ch);

//close curl resource to free up system resources
curl_close($ch);

//build array to store our radio stats for later use
$radio_info = array();
$radio_info['server'] = $SERVER;
$radio_info['title'] = '';
$radio_info['description'] = '';
$radio_info['content_type'] = '';
$radio_info['mount_start'] = '';
$radio_info['bit_rate'] = '';
$radio_info['listeners'] = '';
$radio_info['most_listeners'] = '';
$radio_info['genre'] = '';
$radio_info['url'] = '';
$radio_info['now_playing'] = array();
   $radio_info['now_playing']['artist'] = '';
   $radio_info['now_playing']['track'] = '';

//loop through $ouput and sort into our different arrays
$temp_array = array();

$search_for = "<td\s[^>]*class=\"streamdata\">(.*)<\/td>";
$search_td = array('<td class="streamdata">','</td>');

if(preg_match_all("/$search_for/siU",$output,$matches)) {
   foreach($matches[0] as $match) {
      $to_push = str_replace($search_td,'',$match);
      $to_push = trim($to_push);
      array_push($temp_array,$to_push);
   }
}

//sort our temp array into our ral array
$radio_info['title'] = $temp_array[0];
$radio_info['description'] = $temp_array[1];
$radio_info['content_type'] = $temp_array[2];
$radio_info['mount_start'] = $temp_array[3];
$radio_info['bit_rate'] = $temp_array[4];
$radio_info['listeners'] = $temp_array[5];
$radio_info['most_listeners'] = $temp_array[6];
$radio_info['genre'] = $temp_array[7];
$radio_info['url'] = $temp_array[8];

$x = explode(" - ",$temp_array[9]);
$radio_info['now_playing']['artist'] = $x[0];
$radio_info['now_playing']['track'] = $x[1];

?>

Dose someone know where to insert the script?

Community
  • 1
  • 1
Trance84
  • 165
  • 1
  • 3
  • 10
  • Can you be more specific as to what you are asking? Appropriate `
    ` for what? What info?
    – Brad Aug 15 '12 at 16:36
  • the above script supposed to show me listener stats pulled from xml file in my server...i'm guessing other then put this code in the header.php i need a div to show that stats? im a bit newbie at this obviously... – Trance84 Aug 15 '12 at 16:41

2 Answers2

1

Everything you need will be found in $radio_info. You can easily output this wherever you want:

echo htmlspecialchars($radio_info['now_playing']['artist'] . ' - ' . $radio_info['now_playing']['track']);
Brad
  • 159,648
  • 54
  • 349
  • 530
  • And that line needs to be together with the above script right? whenever i insert the php script in the first post to my header.php..the website wont load...it just hangs..wrong place? (WP site) – Trance84 Aug 15 '12 at 17:00
  • 1
    @Trance84, Chances are that you have the wrong address specified in your code snippet to your radio server. And no, that line doesn't need to be directly with your above script. It can go wherever you need it. I recommend learning some PHP basics before continuing, so that you fully understand what is going on, and how to debug the situation. – Brad Aug 15 '12 at 17:58
  • wow...3 votes down because i'm new to php...nice people in here! anyway m8 the address is correct i probably put it in the wrong section..and the line you gave just acts as a text in the site..i wrapped it with php call but it didn't show anything...i guess ill spend a few weeks learning where to put that right? what else am i doing here ;) thank you for the help m8...not being sarcastic..i really mean it. – Trance84 Aug 15 '12 at 18:14
  • @Trance84, Yes, PHP code needs to be within ``. Otherwise it will just be text. If you didn't see anything, it's because your variables don't contain anything, implying that the script you pasted above isn't working for some reason. I recommend enabling error reporting and seeing what is going on. – Brad Aug 15 '12 at 18:22
  • 1
    Also, I didn't downvote your question, but I suspect that the reason three others did had to do with the clarity of what you are asking, and not to do with the fact that you don't know PHP. "What will be the appropriate div" isn't something we can answer for you. That depends on what div you want the data to be in! Only you can answer that. You should go through some basic PHP tutorials (check out tizag.com) and have some basic familiarity before proceeding. Ask questions here as you go, and make them specific and answerable. You will get quality answers that way. – Brad Aug 15 '12 at 18:23
  • Thanx for the explenation, i changed my question..i ran the script on a new php file and ran error_reporting...i get this Notice: Undefined offset: 1 in /usr/share/airtime/public/ice.php on line 72 line 72 has this: $radio_info['now_playing']['track'] = $x[1]; – Trance84 Aug 15 '12 at 18:38
  • @Trance84, This tells you that there is nothing in `$x[1]`, which means that the script's assumption that there will always be a ` - ` in the metadata was wrong. `echo $temp_array[9]` to see what is actually there. – Brad Aug 15 '12 at 18:41
  • well...i ran the script one more time again and it worked..its now outputting the xml stats...but again..single file i created with only the script on different server..so i guess the location of the script is wrong? its not supposed to be in the header? – Trance84 Aug 15 '12 at 18:44
  • @Trance84, I don't understand what you are asking. You can put your `echo` statements anywhere you want. Do you need to know how to `include()` files? – Brad Aug 15 '12 at 18:45
  • the whole problem im getting is whenever i put the php script on my actual website..in the header.php file..the website wont load..it will just hang and then say This webpage is not available..but when i input the same script to a newly created php file on a different server, the script works, so i am asking..am i doing the right thing, inserting the php script in the header.php in wordpress?! – Trance84 Aug 15 '12 at 18:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15390/discussion-between-brad-and-trance84) – Brad Aug 15 '12 at 19:04
0

We download the stats in xml then process the stats daily buy importing to sql

// download icecast stats
$username = '';
$password = '';
$url = 'localhost:8000/stats';
$file = 'h:\\stream\\stats\\' . date('Ymd_His') . '.xml';

c:\\stream\\curl.exe --user $username:$password $url -o $file;

//Process to sql by looping through the stats folder and get filenames...

foreach(glob($path . "*.xml") as $filename) {
   $content = file_get_contents($filename);
   $filename = substr($filename, strlen($path), -4);
    process($db, $filename, $content);
  }
   function process($db, $filename, $content) {
    $xml = new SimpleXMLElement($content);
    if(!preg_match('/^(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})$/', $filename, 
    $date)) throw new Exception('Filename does not look like a date.');

foreach($xml->source as $source) {
    $date_sql = $db->real_escape_string(sprintf("%04d-%02d-%02d %02d:%02d:%02d", 
    $date[1], $date[2], $date[3], $date[4], $date[5], $date[6]));
    $stream_sql = $db->real_escape_string($source->genre);
    $title_sql = $db->real_escape_string($source->title);
    $listeners_sql = (int)$source->listeners;

    $sql = "REPLACE INTO song_stats SET date_and_time = '$date_sql', stream = 
   '$stream_sql', title = '$title_sql', listeners = $listeners_sql";

    $db->query($sql);
   }


 }