R
R
rusgayfer2022-02-28 00:31:29
PHP
rusgayfer, 2022-02-28 00:31:29

How to download video from VK using PHP?

I made this parser:

$url = 'Ссылка на видео'; // https://vk.com/video-***_***

// 1080p
$re = '/"url1080":"(.*)"/U';
$str = getHTML($url);
preg_match_all($re, $str, $url1080, PREG_SET_ORDER, 0);

// 720p
$re = '/"url720":"(.*)"/U';
$str = getHTML($url);
preg_match_all($re, $str, $url720, PREG_SET_ORDER, 0);

// 480p
$re = '/"url480":"(.*)"/U';
$str = getHTML($url);
preg_match_all($re, $str, $url480, PREG_SET_ORDER, 0);

// 360p
$re = '/"url360":"(.*)"/U';
$str = getHTML($url);
preg_match_all($re, $str, $url360, PREG_SET_ORDER, 0);

// 240p
$re = '/"url240":"(.*)"/U';
$str = getHTML($url);
preg_match_all($re, $str, $url240, PREG_SET_ORDER, 0);

if(isset($url1080[0][1])) {
    $link = $url1080[0][1];
} elseif(isset($url720[0][1])) {
    $link = $url720[0][1];
} elseif(isset($url480[0][1])) {
    $link = $url480[0][1];
} elseif(isset($url360[0][1])) {
    $link = $url360[0][1];
} elseif(isset($url240[0][1])) {
    $link = $url240[0][1];
}

// Создаем Json (мне нужно для своего API)
$json = '{"url":"'.$link.'"}';
$json_d = json_decode($json);
$link = $json_d->url;

$filename = uniqid();

// Скачивание видео на сервер
copy($link, "../../../api/v2/method/" . $filename . ".mp4");

// Смотрим код страницы
function getHTML($url) {
    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HEADER, [
    'Accept-Language: ru'
]);

$html = curl_exec($ch);
curl_close($ch);

return $html;
}


I get a link to an mp4 file successfully, but it won't download. Since they have a link tied to an ip address. Sometimes, upon opening the link, it immediately downloads in the browser, sometimes not. What can be done?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
rusgayfer, 2022-02-28
@rusgayfer

Found a way. You need to download the video also via Curl

// Функция загрузки видео
function videoDownload($url) {
$filename = uniqid();
$fp = fopen($filename . ".mp4", 'w'); // Путь скачивания
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'); // Обязательный параметр, без него не скачивается
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
}

T
ThunderCat, 2022-02-28
@ThunderCat

but it doesn't download.
When something does not work, errors usually occur, including errors that can be forwarded from the api side, and sometimes they are even clearly described. See the contents of the response returned from the server, including the server response code.

N
Nikolai Savelyev, 2022-02-28
@AgentSmith

You also need to specify cookies. They are not in the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question