這是一個PHP功能,讓您從流媒體URL中提取MP3元數據(StreamTitle)。
通常,流服務器icy-metaint在響應中放置一個標題,告訴我們在流中發送元數據的頻率。該函數檢查該響應頭,如果存在,它將用它替換interval參數。
否則,該函數調用關于您的間隔的流式URL,如果沒有任何元數據,則它會再次嘗試從offset參數開始的遞歸。<?php /**
*?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]))?{
break;
}
}
}
$context?=?stream_context_create($opts);
if?($stream?=?fopen($streamingUrl,?'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?getMp3StreamTitle($streamingUrl,?$interval,?$offset?+?$interval,?false);
}
}?else?{
throw?new?Exception("Unable?to?open?stream?[{$streamingUrl}]");
}}var_dump(getMp3StreamTitle('http://str30.creacast.com/r101_thema6',?19200));
我希望這有幫助!