2

So I've been experimenting with PHP's fOpen function and I've had a lot of success with reading sound files. Now, I'm trying to create a sort of "relay" for shoutcast streams.

File_get_contents is probably the poorest way of doing this, since the data is continuous.

Would using php sockets yield better results?

Tl;dr What's the best way to output a continuous stream of audio/mpeg data?

Ion Retroz
  • 21
  • 4

1 Answers1

0

I've done this with PHP and SHOUTcast streams in the past. It's certainly possible, but keep in mind that all of your PHP instances will be making separate connections to your SHOUTcast server. If you're using any PHP web hosting, those servers are typically configured in such a way that running a PHP script for more than a few minutes is impossible. For this to work, you'll need your own server for VPS (or a cooperative hosting provider).

You are correct in that you won't be able to use file_get_contents() for this. Depending on your version of cURL, you might get away with connecting to the SHOUTcast server with it. http://php.net/manual/en/book.curl.php The problem generally is that SHOUTcast servers' status lines in their responses are ICY 200 OK instead of HTTP/1.0 200 OK, which some HTTP clients have trouble with. If cURL does work for you, check out using CURLOPT_WRITEFUNCTION with a callback function to echo data back to the client as it comes in.

If cURL doesn't work, you are going to need to use fsockopen() http://www.php.net/manual/en/function.fsockopen.php to connect to the server and handle the HTTP response yourself.

From there, all you have to do is echo the data back to the client as it comes in.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • I'll test this out and get back to the question. If it works I'll post the working code I'm using. Thanks for the help! – Ion Retroz Sep 05 '13 at 19:10
  • Just a question about cURL, one of the examples of curl_exec mentioned passing the "example.com" URL to the browser. If I'm trying to create this script as a sort of "proxy" so users won't need to display their vulnerable IP addresses in HTML, would that IP's passage to the browser be displayed in, say, a packet-sniffer? To be clear, I'm using the IP vs. a URL in cURL. – Ion Retroz Sep 05 '13 at 19:46
  • "vulnerable IP address in HTML" - what? What makes you think a user's IP address is vulnerable in any way? That's how the internet works. What does HTML have to do with SHOUTcast anyway? And, where are you concerned about packet sniffers? – Brad Sep 05 '13 at 21:19
  • The whole reason I'm writing this script is to DDoS-protect SHOUTCast streams. Rather than linking the sound file as http://ipaddress:port/; you can just use http://shoutcastprotected.web/streamProxy.php?id=352. I was worried about packet sniffers because I don't understand the whole part about cURL passing back the URL to the browser or whatever. Look at "Example #1" here: http://www.php.net/manual/en/function.curl-exec.php – Ion Retroz Sep 05 '13 at 22:58
  • That example comment is very misleading. It doesn't pass the URL to the browser, it passes the contents of the resource at that URL back to the browser. Anything hitting this script will have no knowledge of what it does internally, and what it connects to, unless you expose this information. Also, your script won't do much to protect against DDoS attacks, as someone could just attack your proxy script. In fact, I would argue that this will make the problem worse, since you're proxying the SHOUTcast stream in probably the least efficient way possible. PHP is not the right tool for this job. – Brad Sep 05 '13 at 23:26
  • If you want to prevent DDoS, become good friends with your hosting provider. They will know about these attacks the same time you do anyway. – Brad Sep 05 '13 at 23:32
  • Some users don't have the luxury of hosting providers, like me. I do see your point about efficiency. However, when I say DDoS I mean those tools like "EliteStresser" or whatever tools the youngster scriptkids are using. It simply overloads the bandwidth of an uplink. But since my site is run behind a DDoS-protected proxy IP, I don't need to worry about that IP getting attacked. This script allows me to address a resource on my public network but behind the use of a proxy IP or a CloudFlare-bound (for example) domain. It's all about having your content 24/7 these days. – Ion Retroz Sep 06 '13 at 01:26
  • Even if you're hosting yourself, someone is upstream from you, and you need to be in communication with them to handle DDoS attacks. If you feel you must go the proxy route, rather than building a proxy server, just use one off the shelf such as Nginx. – Brad Sep 06 '13 at 01:41
  • The proxy server is using Nginx – Ion Retroz Sep 08 '13 at 17:37