Created
December 27, 2020 08:35
-
-
Save sonyarianto/c46196df1e5f393a2859240d8dde332a to your computer and use it in GitHub Desktop.
Read Shoutcast or Icecast Stream Metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getStreamMetadata($streamUrl) { | |
$needle = 'StreamTitle='; | |
$ua = 'Dailymate Radio/1.0'; | |
$opts = ['http' => ['method' => 'GET', | |
'header' => 'Icy-MetaData: 1', | |
'user_agent' => $ua] | |
]; | |
$context = stream_context_create($opts); | |
$icyMetaIntFound = false; | |
$icyInterval = -1; | |
$offset = 0; | |
if(($headers = get_headers($streamUrl, 0, $context))) { | |
foreach($headers as $h) { | |
if(!(strpos(strtolower($h), 'icy-metaint:') === false)) { | |
$icyMetaIntFound = true; | |
$icyInterval = explode(':', $h)[1]; | |
break; | |
} | |
} | |
} | |
if(!$icyMetaIntFound) { | |
echo "icy-metaint header not exists!"; | |
return; | |
} | |
if($stream = fopen($streamUrl, 'r', false, $context)) { | |
while($buffer = stream_get_contents($stream, $icyInterval, $offset)) { | |
if(strpos($buffer, $needle) !== false) { | |
fclose($stream); | |
$title = explode($needle, $buffer)[1]; | |
return substr($title, 1, strpos($title, ';') - 2); | |
} | |
$offset += $icyInterval; | |
} | |
} | |
} | |
echo getStreamMetadata('http://sukmben.radiogentara.com:8080/gentarahd'); | |
// var_dump(getStreamMetadata('https://freeuk16.listen2myradio.com/live.mp3?typeportmount=s1_23369_stream_600894294')); | |
// var_dump(getStreamMetadata('http://pu.klikhost.com:7720/;')); |
Nice script @sonyarianto. I called it in an AJAX request, put the result in a span and applied CSS scrolling and ended up with a nice player that is updated every 15 secs with JavaScript setInterval:
@starapple2 it just user agent string
Thanks @sonyarianto . What is the importance in this script? Is it a Web standard?
ya it just to tell what User Agent that access that URL, you can type anything
Nice script @sonyarianto. I called it in an AJAX request, put the result in a span and applied CSS scrolling and ended up with a nice player that is updated every 15 secs with JavaScript setInterval:
wow this is very nice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @sonyarianto . What is the importance in this script? Is it a Web standard?