How to read a radio stream by php and stream it out again

390 views Asked by At

I´m trying to get data from a radio stream (MP3) from http and want to stream it out in https. this is what I tried:

<?php

define(URL, 'Mymp3stream.com');

$ch = curl_init(URL);

curl_setopt_array($ch, array(
  CURLOPT_CONNECTTIMEOUT => 60,
  CURLOPT_FOLLOWLOCATION => TRUE,
  CURLOPT_RETURNTRANSFER => TRUE, 
  CURLOPT_HEADER => TRUE
));

curl_exec($ch);

how can I get it streamed ? the browser doesn´t start any audio output. Even with Headers

header('Content-type: audio/mpeg');
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");

there was no audio in the browser. the page is loading all the time.

I also tried to read the stream into a string buffer and echoed it out with:

$stream=fopen($url, 'rb');
 while(!feof($stream)){
   $buffer=fread($stream, 128);
   echo $buffer;
   flush();
 }
fclose($stream); 

another try was to use get_stream_content after opening the file with fopen. there was a minor success after adding the headers: In Chrome there was showed the audioplayer, but no sound started to play.

1

There are 1 answers

0
Thomas Dupont On

You could try this option :

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

The curl_exec will return the result as array as explain in the doc