title.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Please be aware. This gist requires at least PHP 5.4 to run correctly.
  4. * Otherwise consider downgrading the $opts array code to the classic "array" syntax.
  5. */
  6. function getMp3StreamTitle($streamingUrl, $interval, $offset = 0, $headers = true)
  7. {
  8. $needle = 'StreamTitle=';
  9. $ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36';
  10. $opts = [
  11. 'http' => [
  12. 'method' => 'GET',
  13. 'header' => 'Icy-MetaData: 1',
  14. 'user_agent' => $ua
  15. ]
  16. ];
  17. if (($headers = get_headers($streamingUrl)))
  18. foreach ($headers as $h)
  19. if (strpos(strtolower($h), 'icy-metaint') !== false && ($interval = explode(':', $h)[1]))
  20. break;
  21. $context = stream_context_create($opts);
  22. if ($stream = fopen($streamingUrl, 'r', false, $context))
  23. {
  24. $buffer = stream_get_contents($stream, $interval, $offset);
  25. fclose($stream);
  26. if (strpos($buffer, $needle) !== false)
  27. {
  28. $title = explode($needle, $buffer)[1];
  29. return substr($title, 1, strpos($title, ';') - 2);
  30. }
  31. else
  32. return getMp3StreamTitle($streamingUrl, $interval, $offset + $interval, false);
  33. }
  34. else
  35. throw new Exception("Unable to open stream [{$streamingUrl}]");
  36. }
  37. $current_title = getMp3StreamTitle('http://randio.it:8000/randio.mp3', 19200);
  38. echo $current_title
  39. ?>