How to Force Download UOL Online Video Files

Asked

Viewed 1,845 times

0

I would like to know how to force the download of files in this type of URL

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

I already used a code that performs this with all the download was damaged the file got with 1kbs and does not download right.

1 answer

3


The file has not been damaged, if you open the downloaded file will notice that instead of binary data there will be a message, probably HTML with some detail of error, this occurs because you have not passed the user-agent and other required headers.

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

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 the 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;
}
  • Only a split in the case where this http:// would be the right url and where this in the case.mp4 file would be it renamed right ?

  • in case it would take http:// would be that url I put from Uol would pass to force the download and at the end where the.php file would be the file with new name or the same if I want ne.

  • @Rodrigo edited the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.