So this is my PHP video code
it works in browsers but when I try to get it to play on iPhones and iPads it seems to bring up the play button with x through it.
<?php
header('Content-type: video/mp4');
$homepage = file_get_contents('http://cdn.videos.budtraffic.com/sodaradio/J/Jessie%20J%20-%20Laserlight.mp4');
echo $homepage;
exit;
?>
Explanation on Apple Web site
Short answer :
Because iOS device require the server to support byte-ranges request for videos, and your code does not provide that capability.
Long answer :
When iOS device make a request for a video, they don’t download the whole video. They download only small chunk at a time, to minimize bandwidth usage, and also because the user may want to skip to a later portion of the video without downloading the previous parts. For that purpose, they make byte ranges request. Basically, they tell the server “Hey, give me the portion of the file between the 100th and 200th byte”. Byte-range request use the
Rangeheader, and look like the followingThe server must in that case reply with something like that
So you will need much more than those four php line to support iOS devices. First you will to parse the
Rangeheader. This information is available in the$_SERVER['HTTP_RANGE']global var. You will then need to fetch only that portion of the file and send it to the client. You will also need to populate theContent-Rangeheader.Note that the
Content-Rangeheader include the complete file size, so your code will need to provide that information. Note also that almost every server support that kind of request for static files. If you don’t need to do special treatement to the file, a possible option is to copy the file on the public server and let it do the job for you.Non related comment
Take that as constructive criticism, but your code in its current form will bring your server to its knee. You basically put the whole file in memory before sending it, and worse, you do it for every request. If 100 people download a 100MB video at the same time, you’ll need 10 GB of memory on the server to serve them. That does not scale, not at all.
A much better solution would be to send the file as its read from your CDN, using the readfile function. That way, you only need to use a small chunk of memory, and you can serve a lot more concurrent requests.
EDIT : actually, you also need to respond with a special response code, the 206 partial content. I forgot that in the first version of this answer. Now fixed