Answer the question
In order to leave comments, you need to log in
Downloading videos via VK API?
<?php
define('VK_ACCESS_TOKEN', 'xyz token');
define('VK_API_VERSION', 5.103);
// // //
// Download video from an external server to our server
// // //
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ' techslides.com/demos/sample-videos/small.mp4 ');
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
$curl_result = curl_exec($ch);
curl_close($ch);
// Put the video in the folder with the script
$fp = fopen('small.mp4', 'x');
fwrite($fp, $curl_result);
fclose($fp);
// // //
// Get the URL of the link where to upload the video
// // //
$ch = curl_init();
$parameters = http_build_query([
'access_token' => VK_ACCESS_TOKEN, // access_token / access key
'v' => VK_API_VERSION, // API version
'name' => 'Video title',
'description' => 'A comprehensive description of our first videos.',
'group_id' => 192101047, // group ID
'no_comments' => 0 // allow comments
]);
curl_setopt($ch, CURLOPT_URL, ' https://api.vk.com/method/video.save? ' . $parameters);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch,
$curl_result = json_decode(curl_exec($ch), TRUE); // turn the JSON array returned to us by VK into a normal PHP array
curl_close($ch);
// // //
// Upload video to VK servers
// // //
$ch = curl_init();
$parameters = [ 'video_file' => new CURLFile('small.mp4') ];
curl_setopt($ch, CURLOPT_URL, $curl_result['response']['upload_url']);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, TRUE);
$curl_result = json_decode(curl_exec($ch), TRUE);
curl_close($ch);
if (isset($curl_result['error'])) {
exit('Line ' . __LINE__ . ': Error uploading video to VK servers: ' . $curl_result['error'] . '.');
}
echo 'Video uploaded successfully.';
Writes Successfully uploaded, but does not appear in the group
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question