Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
* Please be aware. This gist requires at least PHP 5.4 to run correctly.
* Otherwise consider downgrading the $opts array code to the classic "array" syntax.
function getMp3StreamTitle($streamingUrl, $interval, $offset = 0, $headers = true)
$needle = 'StreamTitle=';
$ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36';
$opts = [
'http' => [
'method' => 'GET',
'header' => 'Icy-MetaData: 1',
'user_agent' => $ua
if (($headers = get_headers($streamingUrl)))
foreach ($headers as $h)
if (strpos(strtolower($h), 'icy-metaint') !== false && ($interval = explode(':', $h)[1]))
$context = stream_context_create($opts);
if ($stream = fopen($streamingUrl, 'r', false, $context))
while($buffer = stream_get_contents($stream, $interval, $offset)) {
if (strpos($buffer, $needle) !== false)
$title = explode($needle, $buffer)[1];
return substr($title, 1, strpos($title, ';') - 2);
$offset += $interval;
var_dump(getMp3StreamTitle('', 19200));
echo "\n\n";
Copy link

To get a result without the "string(n)" infront of the title, change var_dump to echo in the line at the end of the script.
Thanx for a great script!

Copy link

I tried your code out and for a while it worked but the other day I went to my website and the whole page was blank with only the nav bar. I fixed that but now it's not showing the stream anymore. I know my php is 5.4 or higher.

Copy link

Hello, I’ve been using this snippet in my foreign music internet radio for few months now and its working great… !
I’m not a php programmer, a developer did this work a while ago. I have basic programming skills.
Right now, I ‘m trying to build a new internet radio that will play Greek music.
All the tracks that play there have Greek characters for Artist and Title in their tags.
AIMP player plays the stream and picks up the Artist / Title metadata (directly from the mp3 stream), and displays them correctly in Greek no problem as Artist and Title.
The Greek radio stream is : and the foreign music radio is

Using this php snippet (from fracasula) (which is a part of my API), see here, I modified the calling code for the new Greek radio and created this but it never returns anything!

When I changed the MP3 tags to English characters in one track and called the getGreekSongJson.php then it did work fine, so the function isnt't working with the Greek character set.
Can anybody help me with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment