Skip to content

Instantly share code, notes, and snippets.

@mischa78
Last active December 13, 2019 08:35
Show Gist options
  • Save mischa78/7888b5f495fe4ae054056787b9e17ac6 to your computer and use it in GitHub Desktop.
Save mischa78/7888b5f495fe4ae054056787b9e17ac6 to your computer and use it in GitHub Desktop.
<?php
function get_stream_metadata($streaming_url, $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';
$options = [
'http' => [
'method' => 'GET',
'header' => 'Icy-MetaData: 1',
'user_agent' => $ua
]
];
if(($headers = get_headers($streaming_url)))
{
foreach($headers as $header)
{
if(strpos(strtolower($header), 'icy-metaint') !== FALSE && ($interval = explode(':', $header)[1]))
{
break;
}
}
}
$context = stream_context_create($options);
if($stream = fopen($streaming_url, 'r', FALSE, $context))
{
$buffer = stream_get_contents($stream, $interval, $offset);
fclose($stream);
if(strpos($buffer, $needle) !== FALSE)
{
$title = explode($needle, $buffer)[1];
return substr($title, 1, strpos($title, ';') - 2);
}
else
{
return get_stream_metadata($streaming_url, $interval, $offset + $interval, FALSE);
}
}
else
{
throw new Exception('Unable to open stream ['.$streaming_url.']');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment