php stream file from remote server

11,693

Solution 1

Don't know if you will be able to do this in PHP the way you described.

Let's say we have this HTML:

<video width="320" height="240" controls>
    <source src="playmymovie.php?url=http://domain/video-path/video.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>

So now we have to make a PHP page that handles this ( See Using php to output an mp4 video ):

<?php
$vid_url = isset($_GET['url'])?$_GET['url']:"";
if(empty($vid_url)){
    // trigger 404
    header("HTTP/1.0 404 Not Found");
} else {
    // Get Video

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $vid_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $out = curl_exec($ch);
    curl_close($ch);

    // Set header for mp4
    header('Content-type: video/mp4');
    header('Content-type: video/mpeg');
    header('Content-disposition: inline');
    header("Content-Transfer-Encoding:­ binary");
    header("Content-Length: ".filesize($out));

    // Pass video data
    echo $out;
}
exit();
?>

If it were me, I would test the URL first, make sure you get a 200 status, before trying to pass it back out. You could also trigger the correct status so the browser knows whats going on and can alert the user (or you).

Solution 2

very late answer

$remoteFile = 'blabla.com/video5GB.mp4';
play($remoteFile);

function play($url){
    ini_set('memory_limit', '1024M');
    set_time_limit(3600);
    ob_start();
    if (isset($_SERVER['HTTP_RANGE'])) $opts['http']['header'] = "Range: " . $_SERVER['HTTP_RANGE'];
    $opts['http']['method'] = "HEAD";
    $conh = stream_context_create($opts);
    $opts['http']['method'] = "GET";
    $cong = stream_context_create($opts);
    $out[] = file_get_contents($url, false, $conh);
    $out[] = $httap_response_header;
    ob_end_clean();
    array_map("header", $http_response_header);
    readfile($url, false, $cong);
}
Share:
11,693
MrCoder
Author by

MrCoder

Updated on June 04, 2022

Comments

  • MrCoder
    MrCoder almost 2 years

    Let's say I have a video on a remote server and this its url

    " http://domain/video-path/video.mp4 "

    What is the correct way to stream this video using php with the ability to move the video to the backward or the forward..

    I know how to stream the video using fopen and fread functions but I wanna the player(html5 player) to cache the video so the client can move it to forward or backward .. thanks and sorry for my bad english .

  • MrCoder
    MrCoder almost 9 years
    actually my problem is not about how to stream the video .. my problem is about how to stream the video with the ability to move the video to backward or forward .. it seems that is impossible because fseek function is not supporting remote files it works only with local files .. so i'm search now about streaming the video while being written .. i will make a script to download it to my server and other script to stream it while being written . thanks anyway for your help and i hope u can understand what i meant
  • Twisty
    Twisty almost 9 years
    Maybe you can force a caching in the browser such that the browser can rewind at least: stackoverflow.com/questions/5111382/…
  • Twisty
    Twisty almost 9 years
  • MrCoder
    MrCoder almost 9 years
    thank you but its not possible to do it .. i used another solution .. a file to download the video and another file to stream it while being written on my server ... so now i can use fseek inside the stream file .
  • joseantgv
    joseantgv about 3 years
    $httap_response_header and $http_response_header are undefined vars