开发者

Simple check (PHP) to see if Shoutcast radio is online?

Basically, just a simple script开发者_JS百科 that can check to see if a shoutcast radio is online or not, and output a code based on it.

I tried to do this with file_get_contents and eregi, but it didn't seem to work, or was waaaaay to slow.

Cheers.

:)


Sorki's answer is fine if you just want to determine that the server is running, but as Gumbo pointed out, there are different levels of "online".

For example, the server may be disabled so that it does not accept stream connections. The server could be accepting the stream connections, but the source may be disconnected.

For this, you need to check the status in /7.html. Hit this with "Mozilla" in the user-agent string somewhere. You will get something back like this:

2,1,22,625,2,128,How Far To Austin - Don't Get Me Wrong

The data field are:

listeners, status, peak listeners, maximum listeners, unique listeners, bitrate, track meta

Easy to parse... just do an explode() on it.


Use fsockopen and check for the error.

$fp = fsockopen("www.example.com", 8000, $errno, $errstr, 1); //last param is timeout in seconds
if (!$fp) {
    echo "$errstr ($errno)<br />\n"; // radio offline
} else {
    fclose($fp); // radio OK
}

You have to try and determine the timeout but it might be best to run this with bigger timeout regularly on background with cron and saving the results somewhere.


If it's your radio (you know password and username), you can use a CURL. Try get a $xml->STREAMSTATUS value from that piece of code:

<?php

$useragent    = "Mozilla (DNAS 2 Statuscheck)";
$sc_host    = '192.168.0.1';
$sc_port    = '8000';
$sc_user    = 'admin';
$sc_pass    = 'XXXXX';
$sc_sid        = '1';


$ch = curl_init($sc_host . '/admin.cgi?mode=viewxml&sid=$sc_sid');

curl_setopt($ch, CURLOPT_PORT, $sc_port);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $sc_user . ':' . $sc_pass);

$curl = curl_exec($ch);

if ($curl)
{
    $xml = simplexml_load_string($curl);


   // THIS IS THE ANSWER FOR YOUR QUESTION: 
    var_dump($xml->STREAMSTATUS);

  // if retuns 1 - radio is online
  // if retuns 0 - radio is offline    

}
else
{
    die('Could not connect to dnas-server!');
}
?> 

enjoy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜