How to force downloading of video files online UOL

0

I would like to know how to force files to be downloaded to this type of URL

http://videohd7.mais.uol.com.br/15529034.mp4?p=1&r=http%3A%2F%2Fmais.uol.com.br

I have already used a code that does this with everything the download was damaged, the file got 1kbs and you do not download it right.

    
asked by anonymous 06.07.2015 / 20:37

1 answer

1

The file was not damaged, if you open the downloaded file will note that instead of binary data there will be a message, probably HTML with some error detail, this is because you did not pass the user agent and other necessary headers .

  

Note: About downloading videos from these servers, I personally do not know how copyright issues work, I just posted the answer because it could be a system of personal videos, or something like that, the responsibility of downloaded content is how it will be developed the application.

Using curl do something like (to save the video to your server):

$url = 'http://videohd7.mais.uol.com.br/15529034.mp4?p=1&r=http%3A%2F%2Fmais.uol.com.br';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

//Envia o user-agent do usuário para o dominio
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

//Também pode adicionar como conexão fechada, alguns servidores bloqueiam se não ouver o header Connection
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Connection: close'
));

$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if ($httpcode !== 206 && $httpcode !== 200) {
    echo 'Erro http:', $httpcode;
} else {
   file_put_contents('arquivo.mp4', $data, FILE_BINARY);//A flag FILE_BINARY é necessária
}

If the download is direct to the client (force download in the browser), use:

curl_close($ch);

if ($httpcode !== 206 && $httpcode !== 200) {
    echo 'Erro http:', $httpcode;
} else {
    header('Content-type: video/mp4');
    header('Content-length: ' . strlen($data)); //Seta o tamanho do arquivo
    header('Content-Disposition: attachment; filename=arquivo.mp4');//Força o download
    header('Content-Transfer-Encoding: binary'); //Este header é necessário

    echo $data;
}
    
06.07.2015 / 20:46