title: Restream a Shoutcast/Icecast Stream Using PHP
body: <p>I need to somehow restream the Shoutcast/Icecast stream using the PHP.</p>
  
  <p>Why?</p>
  
  <p>Because Shoutcast/Icecast streams are non https. And it's sent through not 80 and 443 port, but some different strange ports. And I need the https links on normal/standard ports like 80 or 443. This is the biggest reason, although there are some more but less important I think.</p>
  
  <p>These links are like <code>http://hostname.com:5921/stream</code>, and I need links like <code>https://hostname.com/stream?user=x</code> instead.</p>
  
  <p>I made deep research and did not found much.</p>
  
  <p>I found things like:</p>
  
  <pre><code>https://stackoverflow.com/questions/7998773/is-it-possible-to-restream-an-internet-radio-using-php-php-guru-needed
  https://www.svnlabs.com/blogs/radio-icecast-shoutcast-php-proxy-to-re-stream-radio-stream-on-https/
  https://stackoverflow.com/questions/36306457/read-mp3-stream-and-echo-back-to-client-in-php
  </code></pre>
  
  <p>The best code I have collected for now from all resources and my own tries is:</p>
  
  <pre><code>$link = 'http://shoutStreame.streamland.com/proxy/radioGame?mp=/1'; //example link to a Shoutcast stream (not working, only example)
  
  ob_start();
  header("Content-Transfer-Encoding: binary");
  header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
  header('Content-Disposition: attachment; filename="stream.mp3"');
  header('X-Pad: avoid browser bug');
  header('Cache-Control: no-cache');
  $handle = fopen($link, 'r');
  
  while (($data = fread($handle, 1024))) {
      echo $data;
      ob_flush();
      flush();
  }
  </code></pre>
  
  <p>And this code doesn't seems to be... good? excellent?</p>
  
  <p>I just feel like I am doing it wrong way with this code, and it's not efficent and may lead to problems.</p>
  
  <p>My main concerns are:</p>
  
  <ul>
  <li>efficency, especially under many requests</li>
  <li>legal problems? are there any real problems when doing things this way? restreaming using php?</li>
  <li>crash problems? like crash of the whole php, nginx or even machine?</li>
  <li>losing the connection, like this php script will keep dying after a while or something</li>
  </ul>
  
  <p>And there could be maybe more.</p>
  
  <p>It is really hard for me to find any more resources, data and information regarding this particular topic of restreaming the audio stream using the PHP.</p>
  
  <p>For now I do not really know what to do. I am just researching and thinking but as I said, it is really hard to find something more about this topic. And this is the only code I have for now and I do not know if it is good to use it... :)</p>