【问题标题】:IceCast Stats on website网站上的 IceCast 统计数据
【发布时间】:2019-05-26 12:30:05
【问题描述】:

我发现这个 php 脚本从我的流媒体服务器中提取 IceCast 统计数据(听众、当前歌曲)。

在这里发布

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];

?>

有人知道在哪里插入脚本吗?

【问题讨论】:

  • 您能更具体地说明您的要求吗?合适的&lt;div&gt; 用于什么?什么信息?
  • 上面的脚本应该向我显示从我服务器中的 xml 文件中提取的侦听器统计信息...我猜其他然后将此代码放在 header.php 我需要一个 div 来显示该统计信息?我显然是个新手……

标签: php html wordpress icecast


【解决方案1】:

您需要的一切都可以在$radio_info 中找到。您可以轻松地将其输出到任何您想要的位置:

echo htmlspecialchars($radio_info['now_playing']['artist'] . ' - ' . $radio_info['now_playing']['track']);

【讨论】:

  • 那行需要和上面的脚本放在一起吧?每当我将第一篇文章中的 php 脚本插入到我的 header.php 中时 ..网站不会加载...它只是挂起..错误的地方? (WP网站)
  • @Trance84,很可能您的代码中指定了错误的地址 sn-p 到您的无线电服务器。不,该行不需要直接与您的上述脚本一起使用。它可以去任何你需要的地方。我建议在继续之前学习一些 PHP 基础知识,以便您完全了解发生了什么,以及如何调试这种情况。
  • 哇...3 票否决,因为我是 php 新手...这里的人很好!无论如何m8地址是正确的我可能把它放在错误的部分..你给的行只是作为网站中的一个文本..我用php调用包装了它但它没有显示任何东西......我猜我生病了花几个星期学习把它放在哪里对?我还在这里做什么;) 谢谢你的帮助 m8...不是讽刺..我是认真的。
  • @Trance84,是的,PHP 代码需要在&lt;?php?&gt; 内。否则它将只是文本。如果您没有看到任何内容,那是因为您的变量不包含任何内容,这意味着您在上面粘贴的脚本由于某种原因无法正常工作。我建议启用错误报告并查看发生了什么。
  • 另外,我没有否决你的问题,但我怀疑其他三个人的原因与你所要求的内容的明确性有关,而不是与你没有的事实有关不懂PHP。 “什么是合适的 div”不是我们可以为您回答的问题。这取决于您希望数据位于哪个 div 中!只有你能回答这个问题。在继续之前,您应该阅读一些基本的 PHP 教程(查看 tizag.com)并熟悉一些基本知识。随时随地提出问题,并使其具体且可回答。这样,您将获得高质量的答案。
【解决方案2】:

我们下载 xml 中的统计数据,然后处理每日购买的统计数据导入到 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);
   }


 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 2010-09-06
    • 1970-01-01
    • 2021-12-18
    • 2013-03-21
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多