<?php
##################################
##### This document contains a class that will allow you to easily extract data from online radiostations
##### Also, an example is provided at the bottom
#####
##### This class has been written by Martijn Korse and may be freely used, but only for non-profit purposes
##### When in doubt, please contact me : marty[at]excudo[dot]net
##### http://start.excudo.net
#####
##### (ps: a handwritten version of the stripos() function has also been included in this document, for those
##### without PHP 5)
##################################
#
# The Class
#
class Radio
{
var $fields;
var $very_first_str;
function Radio($array=array())
{
$this->setFields($array);
$this->setOffset("Current Stream Information");
}
function setFields($array)
{
$this->fields = $array;
}
function setOffset($string)
{
$this->very_first_str = $string;
}
function getData($url, $port, $display_array=null, $very_first_str=null)
{
if (!isset($display_array))
$display_array = $this->fields;
if (!isset($very_first_str))
$very_first_str = $this->very_first_str;
$domain = (substr($url, 0, 7) == "http://87.212.162.42") ? substr($url, 7) : $url;
// opening a connection and reading the contents
if (@$fp = fsockopen($domain, $port, $errno, $errstr, 2))
{
$contents = "";
fputs($fp, "GET / HTTP/1.1\r\n".
"User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\n".
"Host: ".$domain."\r\n\r\n");
while (!feof($fp))
{
$contents .= fgets($fp, 256);
}
fclose ($fp);
// parsing the contents
foreach ($display_array AS $key => $item)
{
$very_first_pos = stripos($contents, $very_first_str);
$first_pos = stripos($contents, $item, $very_first_pos);
$line_start = strpos($contents, "<td>", $first_pos);
$line_end = strpos($contents, "</td>", $line_start) + 4;
$difference = $line_end - $line_start;
$line = substr($contents, $line_start, $difference);
$data[$key] = strip_tags($line);
}
return $data;
}
else
{
return $errstr." (".$errno.")";
}
}
}
// schaamteloos van php.net gekopieerd
if (!function_exists("stripos"))
{
function stripos($haystack, $needle, $offset=0)
{
return strpos(strtoupper($haystack), strtoupper($needle), $offset);
}
}
#
# The example
#
/******
* $display_array determines the data that will be displayed
* ==============
* Posibilities:
* --------------
* - Server Status
* - Stream Status
* - Listener Peak
* - Average Listen Time
* - Stream Title
* - Content Type
* - Stream Genre
* - Stream URL
* - Stream AIM
* - Stream IRC
* - Current Song
***/
$display_array = array("Current Song");
$radio = new Radio($display_array);
$data_array = $radio->getData("http://87.212.162.42", 8000);
if (is_array($data_array))
{
foreach ($display_array AS $i => $text)
{
if ($text == "http://87.212.162.42:8000") {
$datastring = "<a href="".$data_array[$i]."" target="_blank">".$data_array[$i]."</a>";
} else {
$datastring = $data_array[$i];
echo $text;
echo $datastring;
}
}
}
?>