Answer the question
In order to leave comments, you need to log in
How to upload a document via api vk?
Tell me how to upload a document (gif) to your account via api vk
. In the future, I want to attach the uploaded document to posts posted on the walls of different groups.
Here is my script, tell me what's wrong !!!!
<?php
$token = 'тут токен пользователя '; // токен с правами DOCS
$group = 142929375;
uploadDoc($group,$token);
// получение адреса сервера для загрузки документов
function uploadDoc($group,$token)
{
// 'group_id' => $group, параметр убран из запроса , так как грузим в текущий аккаунт
$url = 'https://api.vk.com/method/docs.getUploadServer';
$params = [
'version' => 5.80,
'access_token' => $token
];
$getUrl = post($url, $params);
if($getUrl)
{
$getUrl = json_decode($getUrl, true);
$url = $getUrl['response']['upload_url']; // URL UPLOAD сервера
// загрузка файла на сервер ВК
define('BASEPATH', str_replace('\\', '/', dirname(__FILE__)) . '/');
$image = BASEPATH.'temp.gif';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, ['photo' => new CURLFile($image, 'image/gif')], true);
$upload = curl_exec( $curl );
curl_close( $curl );
// вывод принтов для наглядности происходящего
print_r('PATH = '.$image.'<br>');
print_r('SERVER = '.$url.'<br>');
print_r('UPLOAD_INFO(file) = '.$upload.'<br>');
if($upload)
{
$upload = json_decode($upload, true);
$file = $upload['file'];
$error = $upload['error'];
$url = 'https://api.vk.com/method/docs.save';
$params = [
'file' => $file,
'title' => 'test',
'tags' => 'test',
'version' => 5.80,
'access_token' => $token
];
$uploadfile = post($url, $params);
print_r('UPLOAD_DOC = '.$uploadfile.'<br>');
}
else{}
}
else{}
}
// отправка запроса в ВК
function post($url, $params) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(!$result){
$result = curl_error($ch);
}
curl_close($ch);
return $result;
}
?>
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